Oracle SQL - 按天计算 - 不是 GROUP BY 表达式
Oracle SQL - count by day - not a GROUP BY expression
我有这样的数据:
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
这意味着,我有一个列 TIME
和 ID
。我想要做的是每天计算所有条目和每天所有不同的 ID
s。
结果我想得到这个:
DAY COUNT(*) distinctID
29/11/20 1 1
06/01/21 2 1
我这样做了:
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');
作为错误,我得到:not a GROUP BY Expression
。
此外,我也不确定我执行的日期操作是否正确。
注意:我想将日期条目用作日期值,而不是比较字符串或类似的东西。
我怎样才能得到我所期望的?
嗯。 . .我想你想要:
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);
我不确定您为什么使用 to_char()
或 'HH'
。如果你真的想把时间输出为'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);
我有这样的数据:
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
这意味着,我有一个列 TIME
和 ID
。我想要做的是每天计算所有条目和每天所有不同的 ID
s。
结果我想得到这个:
DAY COUNT(*) distinctID
29/11/20 1 1
06/01/21 2 1
我这样做了:
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');
作为错误,我得到:not a GROUP BY Expression
。
此外,我也不确定我执行的日期操作是否正确。
注意:我想将日期条目用作日期值,而不是比较字符串或类似的东西。
我怎样才能得到我所期望的?
嗯。 . .我想你想要:
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);
我不确定您为什么使用 to_char()
或 'HH'
。如果你真的想把时间输出为'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);