Oracle SQL-如何检索今天与昨天的ID计数差异
我有一个表,可以捕获客户购买产品的时间。它捕获唯一的购买id以及购买时间戳 我想能够查询,今天和昨天购买的数量之间的差异Oracle SQL-如何检索今天与昨天的ID计数差异,sql,oracle,date,difference,Sql,Oracle,Date,Difference,我有一个表,可以捕获客户购买产品的时间。它捕获唯一的购买id以及购买时间戳 我想能够查询,今天和昨天购买的数量之间的差异 不确定如何在oracle上查询此信息?您可以使用条件聚合: select sum(case when trunc(datecol) = trunc(sysdate - 1) then 1 else 0 end) as num_yesterday, sum(case when trunc(datecol) = trunc(sysdate) then 1 else
不确定如何在oracle上查询此信息?您可以使用条件聚合:
select sum(case when trunc(datecol) = trunc(sysdate - 1) then 1 else 0 end) as num_yesterday,
sum(case when trunc(datecol) = trunc(sysdate) then 1 else 0 end) as num_today,
sum(case when trunc(datecol) = trunc(sysdate) then 1
when trunc(datecol) = trunc(sysdate - 1) then -1
end) as diff
from t
where datecol >= trunc(sysdate - 1);
您可以使用条件聚合:
select sum(case when trunc(datecol) = trunc(sysdate - 1) then 1 else 0 end) as num_yesterday,
sum(case when trunc(datecol) = trunc(sysdate) then 1 else 0 end) as num_today,
sum(case when trunc(datecol) = trunc(sysdate) then 1
when trunc(datecol) = trunc(sysdate - 1) then -1
end) as diff
from t
where datecol >= trunc(sysdate - 1);
您可以使用Group功能将购买日与时间戳信息分组,并计算购买id
select trunc(purchase_ts) Day, count(purchase_id) Count
from purchase
group by trunc(purchase_ts)
order by 1
您可以使用Group功能将购买日与时间戳信息分组,并计算购买id
select trunc(purchase_ts) Day, count(purchase_id) Count
from purchase
group by trunc(purchase_ts)
order by 1
在列上使用
TRUNC
将阻止Oracle在该列上使用索引(相反,您需要一个单独的基于函数的索引);相反,使用CASE
语句测试日期是否介于一天的开始和第二天的开始之间,然后COUNT
计算这些范围之间的值:
选择计数(
案例
当TRUNC(SYSDATE)-间隔“1”天在列上使用TRUNC
时,将阻止Oracle在该列上使用索引(相反,您需要一个单独的基于函数的索引);而是使用CASE
语句测试日期是否介于一天的开始和第二天的开始之间,然后计数这些范围之间的值:
选择计数(
案例
当TRUNC(SYSDATE)-间隔“1”天