我有两个表,表 1:
Col1_1 Col1_2
A X
B Y
C Z
表 2:
Col2_1 Col2_2 Col2_3 Col2_4
A X 6/20/2018 10
B Y 8/13/2018 20
C Z 6/27/2018 10
A X 8/13/2018 40
B Y 9/3/2018 80
C Z 12/1/2018 5
这是我正在寻找的输出(今天的日期是 6/18/2018 ):
Col1_1 Col1_2 Sum of Col2_4 between today and (today + 5 days) Sum of Col2_4 between (today + 5 days) and (today + 10 days)
A X 10 0
B Y 0 0
C Z 0 10
这是我目前在 teradata 中的代码:
SELECT
Col1_1,
Col1_2,
Case
WHEN Col2_3 > CURRENT_DATE and Col2_3 < CURRENT_DATE+5
THEN SUM (Col2_4)
ELSE '0'
End as Calculated_Col_1,
Case
WHEN Col2_3 > CURRENT_DATE+5 and Col2_3 < CURRENT_DATE+10
THEN SUM (Col2_4)
ELSE '0'
End as Calculated_Col_2,
FROM Table1 as A
INNER JOIN Table2 as B
ON A.Col1_1 = B.Col2_1
AND A.Col1_2 = B.Col2_2
WHERE -- ? ?
我被困在如何定义SELECT
语句中的列名以及如何WHERE
定义条件。
任何建议表示赞赏!谢谢!