简体   繁体   English

OrientDB-按日期分组查询

[英]OrientDB - Group by date query

I'm trying to execute a query in OrientDB to get the number of posts per day. 我正在尝试在OrientDB中执行查询以获取每天的帖子数。 However, my GROUP BY query is failing, and I fail to see what I'm doing wrong. 但是,我的GROUP BY查询失败,并且我看不到自己在做什么错。

I have a database filled with 3 Posts, all with a different date. 我有一个数据库,里面满是3条帖子,所有帖子的日期都不同。

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 EDITED

Here's an example in Java: 这是Java中的示例:

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

Output : 输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM