I have 600 Million rows as table 1 below. In Teradata SQL how to transfer "by date" to by "date range"?
+-----------+-------+------------+----------+
| ProductID | Store | Trans_Date | Cost_Amt |
+-----------+-------+------------+----------+
| 20202 | 2320 | 2018-01-02 | $9.23 |
| 20202 | 2320 | 2018-01-03 | $9.23 |
| 20202 | 2320 | 2018-01-04 | $9.23 |
| 20202 | 2320 | 2018-01-05 | $9.38 |
| 20202 | 2320 | 2018-01-06 | $9.38 |
| 20202 | 2320 | 2018-01-07 | $9.38 |
| 20202 | 2320 | 2018-01-08 | $9.23 |
| 20202 | 2320 | 2018-01-09 | $9.23 |
| 20202 | 2320 | 2018-01-10 | $9.23 |
+-----------+-------+------------+----------+
desired output:
+-----------+-------+------------+------------+----------+
| ProductID | Store | Start Date | End Date | Cost_Amt |
+-----------+-------+------------+------------+----------+
| 20202 | 2320 | 2018-01-02 | 2018-01-04 | $9.23 |
| 20202 | 2320 | 2018-01-05 | 2018-01-07 | $9.38 |
| 20202 | 2320 | 2018-01-08 | 2018-01-10 | $9.23 |
+-----------+-------+------------+------------+----------+
I expanded the sample to this:
and this query is used to display the derived table:
which calculates a "grp" value we need later:
and the date ranges are now calculated:
with this as a result:
also see: http://rextester.com/PJRU91378
This isn't very elegant, but it seems to be correct:
Live example is here: http://sqlfiddle.com/#!15/7f8c4/12
If you got one row per
ProductID,Store,Trans_Date
(no missing dates) you can apply proprietary Teradata syntax to normalize overlapping date ranges:There's also a
normalize
option forcreate table
.