OrientDB - 按日期查询分组

OrientDB - Group by date query

我正在尝试在 OrientDB 中执行查询以获取每天的帖子数。但是,我的 GROUP BY 查询失败了,我看不出我做错了什么。

我有一个包含 3 个帖子的数据库,所有帖子的日期都不同。

这是我的查询:

select
    datePosted.format("dd-MM-yyyy") as day, count(*)
from
    Post
group by
    day

然而,它不起作用。我希望它检索一个包含每天帖子数量的结构,但它只检索一个结果:

[#-2:1{count:3} v0]

有什么建议吗?

我创建了这个结构,我认为它与你的相似:

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')

这些是我检索您想要的结果的选项:

第一次查询:

select day, count(*) as posts from (select datePosted.format('yyyy-MM-dd') as day from Post) 
group by day

输出:

----+------+----------+-----
#   |@CLASS|day       |posts
----+------+----------+-----
0   |null  |2016-01-25|2
1   |null  |2016-01-28|1
2   |null  |2016-02-04|1
----+------+----------+-----

第二次查询:

select datePosted.format('yyyy-MM-dd'), count(*) as posts from Post group by datePosted

输出:

----+------+----------+-----
#   |@CLASS|datePosted|posts
----+------+----------+-----
0   |null  |2016-01-25|2
1   |null  |2016-01-28|1
2   |null  |2016-02-04|1
----+------+----------+-----

希望对您有所帮助

已编辑

这是 Java 中的示例:

Java代码:

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();
        }
    }

输出:

Day: 2016-01-25   Posts: 2
Day: 2016-01-28   Posts: 1
Day: 2016-02-04   Posts: 1