需要SQL查询-向后倒数365天

时间:2019-03-08 09:35:43

标签: sql oracle

我已经在论坛上搜索了很多次,但是找不到适合我情况的解决方案。我正在使用Oracle数据库。

我有一张表格,其中按天列出所有订单号和客户编号。看起来像这样:

Day | Customer Nbr | Order Nbr  
2018-01-05 | 25687459 | 256   
2018-01-09 | 36478592 | 398  
2018-03-07 | 25687459 | 1547  
and so on....

现在,我需要一个SQL查询,该查询按日提供给我一张表格和Customer Nbr,并从第1列开始计算过去365天内唯一订单号的数量。

对于上面的示例,结果表应如下所示:

Day | Customer Nbr | Order Cnt  
2019-01-01 | 25687459  | 2  
2019-01-02 | 25687459  | 2  
... 
2019-03-01 | 25687459  | 1  

3 个答案:

答案 0 :(得分:0)

修改

我刚刚看到您澄清了这个问题,我将其解释为: 对于过去一年中的每一天,请显示该日期至1年之间每个客户的订单数量。现在正在研究答案...

最新答案:

对于每位客户,我们计算订单日至订单日前365天之间的记录数...

WITH yourTable AS
(
  SELECT SYSDATE - 1 Day, 'Alex' CustomerNbr FROM DUAL
  UNION ALL
  SELECT SYSDATE - 2, 'Alex' FROM DUAL
  UNION ALL
  SELECT SYSDATE - 366, 'Alex'FROM DUAL

  UNION ALL
  SELECT SYSDATE - 400, 'Alex'FROM DUAL
  UNION ALL
  SELECT SYSDATE - 500, 'Alex'FROM DUAL
  UNION ALL
  SELECT SYSDATE - 1, 'Joe'FROM DUAL
  UNION ALL
  SELECT SYSDATE - 300, 'Chris'FROM DUAL
  UNION ALL
  SELECT SYSDATE - 1, 'Chris'FROM DUAL
)

SELECT Day, CustomerNbr, OrdersLast365Days
FROM yourTable t
OUTER APPLY
(
  SELECT COUNT(1) OrdersLast365Days
  FROM yourTable t2
  WHERE t.CustomerNbr = t2.CustomerNbr
    AND TRUNC(t2.Day) >= TRUNC(t.Day) - 364
    AND TRUNC(t2.Day) <= TRUNC(t.Day)
)
ORDER BY t.Day DESC, t.CustomerNbr;

Last 365 Days' Orders

如果您只想在收到订单的那一天进行报告,那么简单的WHERE子句就足够了:

SELECT Day, CustomerNbr, COUNT(1) OrderCount
FROM <yourTable>
WHERE TRUNC(DAY) >= TRUNC(SYSDATE -364)
GROUP BY Day, CustomerNbr
ORDER BY Day Desc;

如果您想每天进行报告,则需要首先生成它们。这可以通过递归CTE完成,然后将其加入到表中:

WITH last365Days AS
(
  SELECT TRUNC (SYSDATE - ROWNUM + 1) dt
  FROM DUAL CONNECT BY ROWNUM < 365
)

SELECT d.Day, COALESCE(t.CustomerNbr, 'None') CustomerNbr, SUM(CASE WHEN t.CustomerNbr IS NULL THEN 0 ELSE 1 END) OrderCount
FROM last365Days d
LEFT OUTER JOIN <yourTable> t
  ON d.Day = TRUNC(t.Day)
GROUP BY d.Day, t.CustomerNbr
ORDER BY d.Day Desc;

Left Join CTE

答案 1 :(得分:0)

一种方法是为每个客户的所有兴趣天生成值,然后使用相关的子查询:

with dates as (
      select date '2019-01-01' + rownum as dte from dual
      connect by date '2019-01-01' + rownum < sysdate
     )
select d.dte, t.customer_nbr,
       (select count(*)
        from t t2
        where t2.customer_nbr = t.customer_nbr and
              t2.day <= t.dte and
              t2.date > t.dte - 365
       ) as order_cnt
from dates d cross join
     (select distinct customer_nbr from t) ;

答案 2 :(得分:0)

我可能会使用和分析功能来完成它。在windowing子句中,可以指定之前的行数或范围。在这种情况下,我将使用范围。

这将为您提供显示日期前一滚动年度中每一天每一天的订单数量

WITH DATES AS ( 
  SELECT * FROM 
    (SELECT TRUNC(SYSDATE)-(LEVEL-1) AS DAY FROM DUAL CONNECT BY TRUNC(SYSDATE)-(LEVEL-1) >= ( SELECT MIN(TRUNC(DAY)) FROM MY_TABLE )) 
    CROSS JOIN 
    (SELECT DISTINCT CUST_ID FROM MY_TABLE))
SELECT DISTINCT
    DATES.DAY,
    DATES.CUST_ID,
    COUNT(ORDER_ID) OVER (PARTITION BY DATES.CUST_ID ORDER BY DATES.DAY RANGE BETWEEN INTERVAL '1' YEAR PRECEDING AND INTERVAL '1' SECOND PRECEDING) 
FROM
  DATES
LEFT JOIN 
  MY_TABLE 
ON DATES.DAY=TRUNC(MY_TABLE.DAY) AND DATES.CUST_ID=MY_TABLE.CUST_ID
ORDER BY DATES.CUST_ID,DATES.DAY;