上一期

时间:2015-10-01 10:16:19

标签: sql teradata

我有下表,代表每天的客户:

+----------+-----------+
|   Date   | Customers |
+----------+-----------+
| 1/1/2014 |         4 |
| 1/2/2014 |         7 |
| 1/3/2014 |         5 |
| 1/4/2014 |         5 |
| 1/5/2014 |        10 |
| 2/1/2014 |         7 |
| 2/2/2014 |         4 |
| 2/3/2014 |         1 |
| 2/4/2014 |         5 |
+----------+-----------+

我想添加2个额外的列:

  1. 当月客户摘要
  2. 上个月的客户摘要
  3. 这里是理想的结果:

    +----------+-----------+----------------------+------------------------+
    |   Date   | Customers | Sum_of_Current_month | Sum_of_Preceding_month |
    +----------+-----------+----------------------+------------------------+
    | 1/1/2014 |         4 |                   31 |                      0 |
    | 1/2/2014 |         7 |                   31 |                      0 |
    | 1/3/2014 |         5 |                   31 |                      0 |
    | 1/4/2014 |         5 |                   31 |                      0 |
    | 1/5/2014 |        10 |                   31 |                      0 |
    | 2/1/2014 |         7 |                   17 |                     31 |
    | 2/2/2014 |         4 |                   17 |                     31 |
    | 2/3/2014 |         1 |                   17 |                     31 |
    | 2/4/2014 |         5 |                   17 |                     31 |
    +----------+-----------+----------------------+------------------------+
    

    我已经设法通过分区函数的简单求和来计算第3列:

    Select 
      Date,
      Customers, 
      Sum(Customers) over (Partition by (Month(Date)||year(Date) Order by 1) as Sum_of_Current_month
    From table
    

    但是,我找不到计算Sum_of_preceding_month列的方法。

    感谢您的支持。

    阿萨夫

3 个答案:

答案 0 :(得分:2)

前一个月有点棘手。您的Teradata版本是什么,TD14.10支持LAST_VALUE

SELECT 
   dt,
   customers,
   Sum_of_Current_month,
   -- return the previous sum
   COALESCE(LAST_VALUE(x ignore NULLS) 
            OVER (ORDER BY dt 
                  ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
           ,0) AS Sum_of_Preceding_month
FROM 
 (
   SELECT 
     dt,
     Customers, 
     SUM(Customers) OVER (PARTITION BY TRUNC(dt,'mon')) AS Sum_of_Current_month,
     CASE -- keep the number only for the last day in month
       WHEN ROW_NUMBER()
            OVER (PARTITION BY TRUNC(dt,'mon')
                  ORDER BY dt)
          = COUNT(*) 
            OVER (PARTITION BY TRUNC(dt,'mon'))
       THEN Sum_of_Current_month
     END AS x
   FROM tab
 ) AS dt

答案 1 :(得分:0)

我认为使用lag()和聚合子查询可能会更容易。 ANSI标准语法是:

Select t.*, tt.sumCustomers, tt.prev_sumCustomers
From table t join
     (select extract(year from date) as yyyy, extract(month from date) as mm,
             sum(Customers) as sumCustomers,
             lag(sum(Customers)) over (order by extract(year from date), extract(month from date)
                                      ) as prev_sumCustomers
      from table t
      group by extract(year from date), extract(month from date)
     ) tt
     on extract(year from date) = tt.yyyy and extract(month from date) = t.mm;

在Teradata中,这将写成:

Select t.*, tt.sumCustomers, tt.prev_sumCustomers
From table t join
     (select extract(year from date) as yyyy, extract(month from date) as mm,
             sum(Customers) as sumCustomers,
             min(sum(Customers)) over (order by extract(year from date), extract(month from date)
                                       rows between 1 preceding and 1 preceding
                                      ) as prev_sumCustomers
      from table t
      group by extract(year from date), extract(month from date)
     ) tt
     on extract(year from date) = tt.yyyy and extract(month from date) = t.mm;

答案 2 :(得分:0)

试试这个:

 SELECT
     [Date],
     [Customers],
     (SELECT SUM(customers) FROM table WHERE MONTH(dte) = MONTH(tbl.dte)),
     ISNULL((SELECT SUM(customers) FROM table WHERE MONTH(dte) = MONTH(DATEADD(MONTH, -1, tbl.dte))), 0)
 FROM table tbl