Teradata获取前两天的行计数并进行比较

时间:2019-10-16 13:39:21

标签: sql teradata

我正在尝试设置数据检查,我们从表中获取今天和以前日期的行数。由于它不在周末或节假日加载,因此我无法说DATE-1。

我想出了以下几点,以获取上一次约会:

SELECT
LOAD_DATE
,COUNT(LOAD_DATE) RW_COUNT
,ROW_NUMBER() OVER (ORDER BY LOAD_DATE ) AS LOAD_ROWNUM
FROM DATABASE1.TABLE1
WHERE LOAD_DATE >= DATE-6
GROUP BY 1

这将产生日期,进行计数并分配一个行号。

LOAD_DATE   RW_COUNT    LOAD_ROWNUM
2019-10-16  8259        1
2019-10-15  8253        2
2019-10-11  8256        3
2019-10-10  8243        4

我将两个最新日期进行比较。当前最多的将是“当前”,而倒数第二大的将是“ prior”。然后,我想将这样的结果作为结果集:

CURRENT_COUNT   PRIOR_COUNT DIFF_PERCENT
8259            8253        .9927

我的问题是,如何引用前两行并将它们相互比较?除非对此有过多考虑,否则我需要两个附加的SELECT语句:1个带有WHERE子句的行1,另一个是WHERE引用的行2。

我该怎么做?我有两个CTE吗?
最终,我将需要第三个SELECT将两行分开,并检查10%的公差。救命,我正在分析瘫痪。

3 个答案:

答案 0 :(得分:1)

我不知道您想要什么结果集。但是您可以将LAG()与聚合一起使用以获取先前的值。

SELECT LOAD_DATE, COUNT(*) as RW_COUNT,
       LAG(COUNT(*)) OVER (ORDER BY LOAD_DATE) as PREV_RW_COUNT
FROM DATABASE1.TABLE1
WHERE LOAD_DATE >= DATE-6
GROUP BY 1;

您可能只希望两者有所不同。

答案 1 :(得分:1)

您可以使用QUALIFY过滤OLAP函数的结果:

SELECT
   LOAD_DATE
  ,COUNT(LOAD_DATE) AS CURRENT_COUNT
   -- previous day's count
  ,LEAD(RW_COUNT)
   OVER (ORDER BY LOAD_DATE DESC) AS PRIOR_COUNT
-- if your TD version doesn't support LAG/LEAD (i.e. < 16.10)
--,MIN(RW_COUNT) 
-- OVER (ORDER BY LOAD_DATE DESC
--       ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) AS PRIOR_COUNT

   ,CAST(CURRENT_COUNT AS DECIMAL(18,4)) / PRIOR_COUNT AS DIFF_PERCENT
FROM DATABASE1.TABLE1
WHERE LOAD_DATE >= DATE-6
GROUP BY 1
-- return the latest row only
QUALIFY ROW_NUMBER() OVER (ORDER BY LOAD_DATE DESC) = 1

检查10%公差

DIFF_PERCENT BETWEEN 0.9 and 1.1

“与”为“合格”或在情况下

答案 2 :(得分:1)

如果您的TD版本(16.0+?)不支持LEAD/LAG,请尝试一下:

SELECT 
  load_date, 
  RW_COUNT, 
  MAX(RW_COUNT) OVER(
    ORDER BY load_date DESC
    ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING -- Get previous row's value
  ) AS RW_COUNT_prev
FROM (
  SELECT load_date, COUNT(LOAD_DATE) RW_COUNT,
  FROM DATABASE1.TABLE1
  WHERE LOAD_DATE >= DATE-6
  GROUP BY 1
) src