2014-01-08 58 views
3

目前,我有一個表中的數據集的按日期記錄:組,如下圖所示在Teradata的

date  id value 
1-Jan-13 1 100 
2-Jan-13 1 100 
3-Jan-13 1 100 
4-Jan-13 1 200 
5-Jan-13 1 200 
6-Jan-13 1 100 
7-Jan-13 1 100 

我想組記錄根據id和Val和版本記錄與開始日期和結束日期。

所需的輸出:

start date end date id value 
1-Jan-13 3-Jan-13 1 100 
4-Jan-13 5-Jan-13 1 200 
6-Jan-13 7-Jan-13 1 100 
+0

確切地說,你的分組標準是?正如我看到的第一組是三天,其他兩天是兩天。怎麼樣? – Rachcha

+0

即時分組基於id和值。以便我能夠創建一個版本.. – user3171610

+0

也許這個問題給你一個線索 - http://stackoverflow.com/questions/13037749/crystal-reports-need-to-group-by-derived-日期範圍 – Rachcha

回答

0

我不是在Teradata的專家,但你最有可能,因爲窗口函數的支持(特別是ROW_NUMBER),能夠做這樣的事

SELECT MIN(date) start_date, MAX(date) end_date, id, value 
    FROM 
(
    SELECT date, id, value, 
     ROW_NUMBER() OVER (PARTITION BY id ORDER BY date) - 
     ROW_NUMBER() OVER (PARTITION BY id, value ORDER BY date) island 
    FROM table1 
) q 
GROUP BY id, value, island 
ORDER BY start_date, end_date 

輸出示例:

 
| START_DATE | END_DATE | ID | VALUE | 
|------------|------------|----|-------| 
| 2013-01-01 | 2013-01-03 | 1 | 100 | 
| 2013-01-04 | 2013-01-05 | 1 | 200 | 
| 2013-01-06 | 2013-01-07 | 1 | 100 | 

這裏是SQLFiddle演示(這是一個SQL Server的演示,但作爲預期的Teradata應該工作)

+1

有幫助嗎?您的問題需要更多幫助嗎? – peterm

0

的ROW_NUMBER版本可以進一步簡化:modified SQL Fiddle

爲Teradata:

SELECT 
    id,val,MIN(dt),MAX(dt) 
FROM 
(
    SELECT 
     id,val,dt, 
     dt - ROW_NUMBER() OVER (PARTITION BY id ORDER BY val, dt) AS dummy 
    FROM table1 
) AS dt 
GROUP BY 1,2,dummy 

而且有TD13.10中用於處理時間序列數據的一些幾乎不知道的函數:

WITH cte(id,val,pd) AS 
(
    SELECT id, val, PERIOD(dt, dt+1) AS pd 
    FROM table1 
) 
SELECT 
    id, val, 
    BEGIN(pd) AS start_dt, 
    LAST(pd) AS end_dt 
FROM 
    TABLE (TD_NORMALIZE_MEET 
      (NEW VARIANT_TYPE(cte.id,cte.val) 
      ,cte.pd) 
    RETURNS (id INTEGER 
      ,val INTEGER 
      ,pd PERIOD(DATE) 
      ,Nrm_Count INTEGER) 
    HASH BY id 
    LOCAL ORDER BY id, val, pd 
    ) A 
ORDER BY start_dt, end_dt