简体   繁体   English

我想按小时,分钟,天分组。 这个怎么做?

[英]I want to group by hour,min,day. how to do this?

I have a table: 我有一张桌子:

CREATE TABLE SYSTEM.DATA
(
    USER VARCHAR2(20 BYTE),
    TIME  DATE,
);

INSERT INTO DATA ("USER", "TIME") VALUES ('A', '2013/3/24 AM 04:00:45');
INSERT INTO DATA ("USER", "TIME") VALUES ('B', '2013/03/24 PM 03:51:18');
INSERT INTO DATA ("USER", "TIME") VALUES ('C', '2013/03/24 PM 03:57:49');
INSERT INTO DATA ("USER", "TIME") VALUES ('D', '2013/03/25 AM 10:05:30');
INSERT INTO DATA ("USER", "TIME") VALUES ('E', '2013/03/25 AM 10:11:30');

How do I get the number of per day(being with today AM7:30,end with tomorrow AM7:29)?like this 我如何获得每天的数量(今天是AM7:30,明天是AM7:29)?

DATE   | COUNT
03/23  |     1   ~~~THIS IS 'A', '2013/3/24 AM 04:00:45'
03/24  |     2
03/25  |     2

Subtract 7.5 hours from "time" and use that for aggregation: 从“时间”中减去7.5小时,并将其用于汇总:

select to_char("time" - 7.5/24, 'YYYY-MM-DD') as thedate, count(*)
from "data"
group by to_char("time" - 7.5/24, 'YYYY-MM-DD')
order by 1

This is the solution for MS Sql Server. 这是MS Sql Server的解决方案。 (I am not familiar with Oracle, but I guess that something similar is possible there as well.) @date_begin and @date_end are the parameters that you can use for the interval of dates for which you want to get the results. (我对Oracle不熟悉,但是我想那里也可能有类似的东西。) @date_begin@date_end是可用于要获取结果的日期间隔的参数。 This solution is different from what Gordon Linoff suggested in that it will return zeroes for dates for which there are no items in the data table, whereas his query will return only dates with positive values. 此解决方案与Gordon Linoff建议的不同之处在于,对于data表中没有项目的日期,该方法将返回零,而其查询将仅返回具有正值的日期。

    with dates (date_item) as
    (
        select dateadd(minute,450,cast(@date_begin as datetime)) as date_item
        union all
        select dateadd(dd,1,d.date_item) as date_item from dates d where d.date_item<@date_end
    )
    select
        dateadd(day, 0, datediff(day, 0, dates.date_item)),
        sum(case when data.[time]>=dates.date_item and data.[time]<dateadd(day,1,dates.date_item) then 1 else 0 end)
    from dates
    left outer join data on 1=1
    group by
        dates.date_item;

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

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