Oracle SQL - 在没有数据时需要虚拟日期值

时间:2014-06-18 17:55:55

标签: sql database oracle select

我查询'销售'表格为'价格',按产品名称分组:

SELECT product_name, sale_price, sale_date,
SUM(CASE WHEN sales.sale_date = TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (sale_price) ELSE 0 END) sale_yday,
SUM(CASE WHEN sales.sale_date = TO_DATE ('14-JUN-14', 'DD-MON-YY') - 7 THEN (sale_price) ELSE 0 END) sale_lweek,

-- below is 10 week slope
-- (N * Sum_XY - Sum_X * Sum_Y)/(N * Sum_X2 - Sum_X * Sum_X)
((COUNT(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (1)
END) * SUM(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (TO_NUMBER(TO_CHAR(sale_date, 'J')) * sale_price) END) - 
SUM(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (TO_NUMBER(TO_CHAR(sale_date, 'J'))) END) * 
SUM(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (sale_price) END))/
(COUNT(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (1)
END) * 
SUM(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (POWER(TO_NUMBER(TO_CHAR(sale_date, 'J')),2)) END) - 
POWER(SUM(CASE WHEN sale_date < TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1 THEN (TO_NUMBER(TO_CHAR(sale_date, 'J'))) END),2))
) slope

FROM sales
WHERE sales.sale_date IN
    (TO_DATE ('14-JUN-14', 'DD-MON-YY'),
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 1,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 2 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 3 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 4 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 5 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 6 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 7 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 8 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 9 * 7,
    TO_DATE ('14-JUN-14', 'DD-MON-YY') - 10 * 7)
GROUP BY sales.product_name

但是,只要前几周没有product_name的数据存在,我就会遇到问题。斜率方程基本上只需要将那些缺失的日子视为“零”和“零”。价值,并相应插入正确的朱利安日作为'sale_date&#39;。有没有什么方法可以检查过去的sale_date是否存在,如果他们没有,那么他们就不会创建&#39; dummy&#39;它的行?

2 个答案:

答案 0 :(得分:4)

这太复杂了,而且不必要。我看到你正在计算最小二乘线性回归的斜率。您是否知道Oracle具有相同的功能REGR_SLOPE但是要回答您的直接问题,请尝试删除WHERE条款并使用OUTER JOIN进行抽样。

...
FROM sales
RIGHT OUTER JOIN 
  ( SELECT dt - offset sale_date
      FROM (SELECT DATE '2014-06-14' dt FROM DUAL)
      CROSS JOIN (          SELECT 0 offset FROM DUAL
                  UNION ALL SELECT 1        FROM DUAL
                  UNION ALL SELECT 1*7      FROM DUAL
                  UNION ALL SELECT 2*7      FROM DUAL
                  UNION ALL SELECT 3*7      FROM DUAL
                  UNION ALL SELECT 4*7      FROM DUAL
                  UNION ALL SELECT 5*7      FROM DUAL
                  UNION ALL SELECT 6*7      FROM DUAL
                  UNION ALL SELECT 7*7      FROM DUAL
                  UNION ALL SELECT 8*7      FROM DUAL
                  UNION ALL SELECT 9*7      FROM DUAL
                  UNION ALL SELECT 10*7     FROM DUAL)) sample
ON sales.sale_date = sample.sale_date
GROUP BY sales.product_name;

答案 1 :(得分:0)

通常,当没有数据时,值存储在那里。 您可以使用NVL函数在日期为NULL时使用虚拟日期。