I'm trying to execute a query in OrientDB to get the number of posts per day. However, my GROUP BY query is failing, and I fail to see what I'm doing wrong.
I have a database filled with 3 Posts, all with a different date.
This is my query:
select
datePosted.format("dd-MM-yyyy") as day, count(*)
from
Post
group by
day
However, it doesn't work. I would expect it to retrieve a structure with the number of posts per day, but it just retrieves only one result:
[#-2:1{count:3} v0]
Any suggestions?
I created this structure and I think that's similar to yours:
create class Post
create property Post.datePosted date
insert into Post (datePosted) values ('2016-01-25')
insert into Post (datePosted) values ('2016-01-28')
insert into Post (datePosted) values ('2016-01-25')
insert into Post (datePosted) values ('2016-02-04')
These are my options to retrieve the results you want:
First query :
select day, count(*) as posts from (select datePosted.format('yyyy-MM-dd') as day from Post)
group by day
Output :
----+------+----------+-----
# |@CLASS|day |posts
----+------+----------+-----
0 |null |2016-01-25|2
1 |null |2016-01-28|1
2 |null |2016-02-04|1
----+------+----------+-----
Second query :
select datePosted.format('yyyy-MM-dd'), count(*) as posts from Post group by datePosted
Output :
----+------+----------+-----
# |@CLASS|datePosted|posts
----+------+----------+-----
0 |null |2016-01-25|2
1 |null |2016-01-28|1
2 |null |2016-02-04|1
----+------+----------+-----
Hope it helps
EDITED
Here's an example in Java:
Java Code :
private static String remote = "remote:localhost/";
public static void main(String[] args) {
String dbName = "DBname";
String path = remote + dbName;
OServerAdmin serverAdmin;
try {
serverAdmin = new OServerAdmin(path).connect("root", "root");
if (serverAdmin.existsDatabase()) { // if DB already exists
System.out.println("Database '" + dbName + "' already exists");
ODatabaseDocumentTx db = new ODatabaseDocumentTx(path);
db.open("root", "root");
Iterable<ODocument> results = db
.command(new OSQLSynchQuery<ODocument>(
"select day, count(*) as posts from (select datePosted.format('yyyy-MM-dd') as day from Post) group by day"))
.execute();
for (ODocument result : results) {
System.out.println("Day: " + result.field("day") + " Posts: " + result.field("posts"));
}
db.close();
}
else {
serverAdmin.createDatabase(dbName, "document", "plocal");
System.out.println("Database " + dbName + " created");
}
serverAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Output :
Day: 2016-01-25 Posts: 2
Day: 2016-01-28 Posts: 1
Day: 2016-02-04 Posts: 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.