Reputation: 37
I have a query that I am using and I need to get a the value of the previous 12 months, 3 months and last month, yet do not know how to do it.
The current query I have is the following:
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
I tried adding as another column the following for the "12 months" one:
(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)
)
However that gave me the error:
[Teradata][ODBC Teradata Driver][Teradata Database] Syntax error: expected something between the 'and' keyword and the 'BETWEEN' keyword.
Any advice on how to get the data I am looking for would be greatly appreciated.
Upvotes: 0
Views: 1956
Reputation: 3833
You can use conditional aggregation logic and the ADD_MONTHS
function. Here's the general idea:
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
So with your query, it would look like:
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
You can adapt similar logic to your other SUM()
s as needed. It's still a normal SUM
except you're choosing when to include a row's value in your calculation based on the invoice_date
.
Upvotes: 2