(Teradata)获取之前12个月,3个月和1个月的SUM值

时间:2019-10-24 13:47:52

标签: sql teradata

我正在使用一个查询,我需要获取前12个月,3个月和上个月的值,但不知道该怎么做。

我当前的查询如下:

select top 100 
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type
, SUM(B.each_ship_qty) as each_ship_qty
, SUM(B.variable_wt_qty) as variable_wt_qty
, SUM(B.ext_cost_amt) as ext_cost_amt
from DB.DC_INVOICE A
inner join DB.DC_INVOICE_LINE B ON A.invoice_nbr = B.invoice_nbr and A.invoice_date = B.invoice_date and A.ship_to_store_nbr = B.ship_to_store_nbr
inner join DB.item C on B.item_nbr = C.item_nbr
left join DB.purchase_order D on B.po_nbr = D.po_nbr
--where A.invoice_date = '2019-09-05' and A.ship_from_dc_nbr = 6011
group by 
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type

我尝试将“ 12个月”中的以下内容添加为另一列:

(SELECT SUM(vnpk_qty)
    FROM DB.item C
    inner join DB.DC_INVOICE_LINE B ON B.item_nbr = C.item_nbr
    inner join DB.DC_INVOICE A ON A.invoice_nbr = B.invoice_nbr and A.ship_to_store_nbr = B.ship_to_store_nbr and BETWEEN DATEADD(dd, A.invoice_date,-365) AND  DATEADD(dd, A.invoice_date,-1)
    )

但是这给了我错误:

[Teradata][ODBC Teradata Driver][Teradata Database] Syntax error: expected something between the 'and' keyword and the 'BETWEEN' keyword.

任何有关如何获取所需数据的建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用条件聚合逻辑和ADD_MONTHS函数。这是一般的想法:

SELECT 
  SUM(
    CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -12) THEN vnpk_qty END
  ) AS sum_12month,
  SUM(
    CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -3) THEN vnpk_qty END
  ) AS sum_3month,
  SUM(
    CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -1) THEN vnpk_qty END
  ) AS sum_1month
FROM DB.DC_INVOICE A

对于您的查询,它看起来像:

select top 100 
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type
, SUM(B.each_ship_qty) as each_ship_qty
, SUM(B.variable_wt_qty) as variable_wt_qty
, SUM(B.ext_cost_amt) as ext_cost_amt
-- New fields
SUM(
  CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -12) THEN C.vnpk_qty END
) AS sum_12month,
SUM(
  CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -3) THEN C.vnpk_qty END
) AS sum_3month,
SUM(
  CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -1) THEN C.vnpk_qty END
) AS sum_1month
-- End new fields
from DB.DC_INVOICE A
inner join DB.DC_INVOICE_LINE B ON A.invoice_nbr = B.invoice_nbr and A.invoice_date = B.invoice_date and A.ship_to_store_nbr = B.ship_to_store_nbr
inner join DB.item C on B.item_nbr = C.item_nbr
left join DB.purchase_order D on B.po_nbr = D.po_nbr
--where A.invoice_date = '2019-09-05' and A.ship_from_dc_nbr = 6011
group by 
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type

您可以根据需要将类似的逻辑应用于其他SUM()。仍然是正常的SUM,只是您要根据invoice_date选择何时在计算中包括行的值。