Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle SQL-如何检索今天与昨天的ID计数差异_Sql_Oracle_Date_Difference - Fatal编程技术网

Oracle SQL-如何检索今天与昨天的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

我有一个表,可以捕获客户购买产品的时间。它捕获唯一的购买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 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”天