我正在为以下场景寻找Teradata SQL Query帮助。
我有一个包含客户ID和交易日期和金额字段的表格,并希望获得所需的输出,包括开始日期和结束日期,以及每个客户在这些日期之间花费的金额。
对于第一笔交易的开始日期,我想硬编码为2000-01-01
Custid Date Amount
1 2014-10-11 $100
1 2014-11-01 $50
1 2015-01-05 $20
2 2016-01-01 $40
2 2016-05-01 $60
我希望输出类似下面的内容
CustID Start_Date End_Date Amount
1 2000-01-01 2014-10-11 $100
1 2014-10-11 2014-11-01 $50
1 2014-11-01 2015-01-05 $20
2 2000-01-1 2016-01-01 $40
2 2016-01-01 2016-05-01 $60
有人可以帮助Teradata中的查询
答案 0 :(得分:1)
你可以使用Window Functions来关闭它(基本上这就像其他RDBMS中的Lag()
功能一样):
SELECT
CustId,
COALESCE(MAX("Date") OVER (PARTITION BY CustId ORDER BY "Date" ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), DATE '2000-01-01') AS Start_Date,
"Date" as End_Date,
Amount
FROM
yourtable;
使用示例:
CREATE MULTISET VOLATILE TABLE mytable(
Custid INTEGER NOT NULL
,"Date" DATE NOT NULL
,Amount VARCHAR(5) NOT NULL
) PRIMARY INDEX (CustID) ON COMMIT PRESERVE ROWS;
INSERT INTO mytable(Custid,"Date",Amount) VALUES (1,'2014-10-11','$100');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (1,'2014-11-01','$50');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (1,'2015-01-05','$20');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (2,'2016-01-01','$40');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (2,'2016-05-01','$60');
SELECT
CustId,
COALESCE(MAX("Date") OVER (PARTITION BY CustId ORDER BY "Date" ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), DATE '2000-01-01') AS Start_Date,
"Date" as End_Date,
Amount
FROM
mytable;
DROP TABLE mytable;
+--------+------------+------------+--------+
| Custid | Start_Date | End_Date | Amount |
+--------+------------+------------+--------+
| 1 | 1/1/2000 | 10/11/2014 | $100 |
| 1 | 10/11/2014 | 11/1/2014 | $50 |
| 1 | 11/1/2014 | 1/5/2015 | $20 |
| 2 | 1/1/2000 | 1/1/2016 | $40 |
| 2 | 1/1/2016 | 5/1/2016 | $60 |
+--------+------------+------------+--------+
您可以选择在SELECT
子句中使用相关子查询:
SELECT
CustId,
COALESCE((SELECT MAX("Date") FROM myTable WHERE mt.CustId = CustID AND "Date" < mt."Date"), DATE '2000-01-01') AS Start_Date,
"Date" AS End_Date,
amount AS amount
FROM
mytable mt;
使用示例:
CREATE MULTISET VOLATILE TABLE mytable(
Custid INTEGER NOT NULL
,"Date" DATE NOT NULL
,Amount VARCHAR(5) NOT NULL
) PRIMARY INDEX (CustID) ON COMMIT PRESERVE ROWS;
INSERT INTO mytable(Custid,"Date",Amount) VALUES (1,'2014-10-11','$100');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (1,'2014-11-01','$50');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (1,'2015-01-05','$20');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (2,'2016-01-01','$40');
INSERT INTO mytable(Custid,"Date",Amount) VALUES (2,'2016-05-01','$60');
SELECT
CustId,
COALESCE((SELECT MAX("Date") FROM myTable WHERE mt.CustId = CustID AND "Date" < mt."Date"), DATE '2000-01-01') AS Start_Date,
"Date" AS End_Date,
amount AS amount
FROM
mytable mt;
DROP TABLE mytable;
+--------+------------+------------+--------+
| Custid | Start_Date | End_Date | amount |
+--------+------------+------------+--------+
| 2 | 2016-01-01 | 2016-05-01 | $60 |
| 1 | 2014-11-01 | 2015-01-05 | $20 |
| 2 | 2000-01-01 | 2016-01-01 | $40 |
| 1 | 2014-10-11 | 2014-11-01 | $50 |
| 1 | 2000-01-01 | 2014-10-11 | $100 |
+--------+------------+------------+--------+