1

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

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

2 回答 2

2

从“时间”中减去 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
于 2013-07-09T02:58:34.273 回答
0

这是 MS Sql Server 的解决方案。(我不熟悉 Oracle,但我想那里也可能有类似的东西。)@date_begin并且@date_end是您可以用于要获得结果的日期间隔的参数。此解决方案与 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;
于 2013-07-09T04:58:10.550 回答