2015-09-18 40 views
1

列我有以下格式一段時間內收集的數據表:頂部結果作爲每一個獨立項目

Title | Date  | Value 
----------------------- 
test1 | 2015-09-18 | 99 
test1 | 2015-09-02 | 97 
test1 | 2015-08-31 | 101 
test1 | 2015-08-03 | 11 
test1 | 2015-07-20 | 100 
test2 | 2015-09-05 | 102 
test2 | 2015-09-04 | 101 
test2 | 2015-08-22 | 91 
test2 | 2015-07-19 | 76 
test2 | 2015-07-12 | 66 

我想能夠輸出最後的結果每月在列中爲每個不同的值。欄目標題(9月,8月,7月)並不重要。總會有3個月的時間跨度。

Title | Sept | Aug | July 
---------------------------- 
test1 | 99 | 101 | 100 
test2 | 102 | 91 | 76 

這可能嗎?我考慮過使用CTE,但對於下一步我有點困惑。

任何幫助/建議表示讚賞!

+0

因此,表格中總有3個*** ****個月的數據。 – Amit

+0

對不起,源表的數據跨越了多年。我想通過最短和最長的日期,這將始終跨越3個月。 – scgough

回答

1

您可以使用CTE來幫助爲實際的SELECT準備結果集。

WITH cte AS (
    SELECT Title, MAX([Date]) d, ROW_NUMBER() OVER(
    PARTITION BY Title ORDER BY DATEFROMPARTS(YEAR([Date]), MONTH([Date]), 1) DESC) rn 
    FROM t1 
    GROUP BY Title, DATEFROMPARTS(YEAR([Date]), MONTH([Date]), 1) 
) 
SELECT t.Title, MAX(CASE WHEN cte.rn = 1 THEN t.Value END) m1, 
    MAX(CASE WHEN cte.rn = 2 THEN t.Value END) m2, 
    MAX(CASE WHEN cte.rn = 3 THEN t.Value END) m3 
FROM cte INNER JOIN t1 t ON 
    t.Title = cte.Title AND t.[Date] = cte.d 
GROUP BY t.Title 

見工作SQLFiddle

*注:這將只需要在過去3個月每「標題」的數據,而是將未對齊如果例如上個月test1的是9月和test2的最後一個月是8月份。

+0

這是我的首選解決方案。謝謝。我可以將它適應任何指定的三個月範圍,因爲我將有一個有效的「結束日期」來處理,因此可以根據需要調整'CASE WHEN cte.rn = 2(,3,4,5,6)'部分。 – scgough

1

做一個GROUP BY,使用CASE爲每個所需的月份做條件MAX

select title, 
     max(case when Month(Date) = 9 then value end), 
     max(case when Month(Date) = 8 then value end), 
     max(case when Month(Date) = 7 then value end) 
from tablename 
group by title 
+1

'月份(日​​期)= 9那麼...'等等 – Mihai

+1

@Mihai,非常感謝!我已更新。 – jarlh

+0

這與直接選擇期望值幾乎一樣無用。你需要最後3個月,而不是所有年份的9,8和7個月。 – Amit

1

您可以使用row_number得到每個月的最後日期爲每個分區中的第一行。

select title, max(case when datepart(mm,datecol) = 9 then value end) as sep, 
       max(case when datepart(mm,datecol) = 8 then value end) as aug, 
       max(case when datepart(mm,datecol) = 7 then value end) as jul 
from (
     select *, 
     row_number() over(partition by title,datepart(mm,datecol) order by datecol desc) as rn 
     from t1) t 
where rn = 1 
group by title; 

Fiddle with sample data

+0

這不會生成請求的輸出。 meh ... – Amit

+0

@ Amit ..需要pivoting..modified我的查詢。 –

+0

當10月份的數據進入時,你預計幾周內會發生什麼?或者如果(如OP所明確指出的那樣)有來自早年的數據? – Amit

0

如果你想要的值擺動在過去三個月:

select title, 
     max(case when year(datecol) = year(getdate()) and month(datecol) = month(getdate()) - 2 
       then value end) as months_2, 
     max(case when year(datecol) = year(getdate()) and month(datecol) = month(getdate()) - 1 
       then value end) as months_1, 
     max(case when year(datecol) = year(getdate()) and month(datecol) = month(getdate()) 
       then value end) as months_0 
from table t 
group by title; 

編輯:

哎呀,我不明白了原來的問題。下面是修改後的代碼:

select title, 
     max(case when seqnum = 1 and year(datecol) = year(getdate()) and month(datecol) = month(getdate()) - 2 
       then value end) as months_2, 
     max(case when seqnum = 1 and year(datecol) = year(getdate()) and month(datecol) = month(getdate()) - 1 
       then value end) as months_1, 
     max(case when seqnum = 1 and year(datecol) = year(getdate()) and month(datecol) = month(getdate()) 
       then value end) as months_0 
from (select t.*, 
      row_number() over (partition by title, year(datecol), month(datecol) 
           order by datecol desc) as seqnum 
     from table t 
    ) t 
group by title; 

在這種情況下,max()是不是真的在做什麼,只是旋轉的值。

+0

這很好,但它會讓我獲得給定月份的** max **值,而不是每個月收集的**最後**值。 – scgough

相關問題