对于以下查询,我得到了下面的结果
select date, count(sales)
from table
where date between to_date('2015-09-01','YYYY-MM-DD') and to_date('2015-12-31','YYYY-MM-DD')
group by date
Date Count(sales)
01-SEP-15 480
01-SEP-15 2
01-SEP-15 3
01-SEP-15 2
16-SEP-15 12
16-SEP-15 7
它应该只给我两行 - > 01-SEP-15和计数(销售额)为487。 和16-SEP-15和计数(销售额)为19 我怎么做到的?
答案 0 :(得分:2)
在Oracle中,date
还包含时间部分。您的SQL客户端通过不在输出中包含它来隐藏它。您需要trunc()
日期列将时间部分设置为00:00:00
select trunc(date), count(sales)
from table
where date between to_date('2015-09-01','YYYY-MM-DD') and to_date('2015-12-31','YYYY-MM-DD')
group by trunc(date)
答案 1 :(得分:1)
您可能想要的逻辑更像是:
select trunc(date), count(sales)
from table
where trunc(date) between date '2015-09-01' and date '2015-12-31'
group by trunc(date);
但是,如果您的索引位于date
但不是trunc(date)
,则可以更自然地将其编写为性能:
select trunc(date), count(sales)
from table
where date >= date '2015-09-01' and
date < date '2016-01-01'
group by trunc(date);