简体   繁体   English

Oracle SQL - 按天计数 - 不是 GROUP BY 表达式

[英]Oracle SQL - count by day - not a GROUP BY expression

I have data which looks like this:我有看起来像这样的数据:

TIME ID 
29/11/20 13:45:33,810000000 1234
06/01/21 13:45:33,810000000 5678
06/01/21 14:05:33,727000000 5678

That means, I have a column TIME and ID .这意味着,我有一列TIMEID What I want to do is to count all the entries by day and all the distinct ID s per day.我想要做的是按天计算所有条目和每天所有不同的ID

As result I would like to get this:结果我想得到这个:

DAY COUNT(*) distinctID
29/11/20 1 1
06/01/21 2 1

I did this:我这样做了:

select trunc(to_char(TIME, ‘HH’),'DD/MM/YY'), 
COUNT(*), count(distinct ID) as distinctID from CDRHEADER 
where TIME>= date '2021-03-01'
group by trunc(TIME,'DD/MM/YY')
order by trunc(TIME,'DD/MM/YY');

As error I get: not a GROUP BY Expression .作为错误我得到: not a GROUP BY Expression

Furthermore, I am also not sure about the date operations I executed and if they are correct.此外,我也不确定我执行的日期操作以及它们是否正确。

NOTE: I would like to use the date entries as date values and not compare strings or something like this.注意:我想将日期条目用作日期值,而不是比较字符串或类似的东西。

How can I get what I expect?我怎样才能得到我所期望的?

Hmmm.嗯。 . . . . I think you want:我想你想要:

select trunc(time) as the_date, 
       count(*), count(distinct ID) as distinctID
from CDRHEADER 
where time >= date '2021-03-01'
group by trunc(time)
order by trunc(time);

I'm not sure why you are using to_char() or 'HH' .我不确定您为什么使用to_char()'HH' If you really want to output the time as 'DD/MM/YYYY' , then:如果你真的想把 output 的时间设为'DD/MM/YYYY' ,那么:

select to_char(trunc(time), 'DD/MM/YYYY') as the_date, 
       count(*), count(distinct ID) as distinctID
from CDRHEADER 
where time >= date '2021-03-01'
group by trunc(time)
order by trunc(time);

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

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