2015-01-13 40 views
0

我有一個返回以下功能:獲取兩個日期之間的月TSQL

Title  Start   End 
Task A 2015-01-02  2015-03-31 
Task B 2015-02-12  2015-04-01 
Task C 2014-11-01  2015-02-05 
.... 

我想每月返回一列,1,如果開始和結束期,否則爲0

內的
Title  Start   End   Jan Feb Mar Apr May Jun .... 
Task A 2015-01-02  2015-03-31 1  1 1 0 0 0 
Task B 2015-02-12  2015-04-01 0  1 1 1 0 0 
Task C 2014-11-01  2015-02-05 1  1 0 0 0 0 
.... 

任何人有關於如何做到這一點的想法?

+2

你在問什麼叫做PIVOT表。如果你看起來有點你會找到你的答案。或者至少如何開始解決你的問題,那麼如果你仍然不能發佈你試過的查詢,我們將幫助你解決它 –

+0

是否有可能在不同年份開始和結束日期,或者你約束查詢到一年? – dazedandconfused

+0

這不是數據透視表。我沒有諸如Jan,Feb等的數據來重複。只需創建基於開始日期和結束日期的列 – SharkTiles

回答

1

你會基本case語句做到這一點:

select title, start, end, 
     (case when 1 between month(start) and month(end) then 1 else 0 end) as jan, 
     (case when 2 between month(start) and month(end) then 1 else 0 end) as feb, 
     . . . 
     (case when 12 between month(start) and month(end) then 1 else 0 end) as dec 
from table t; 

注:我離開你的列名在查詢,即使有些是保留字,應該逃脫(如果是這樣的真實姓名的列)。

另請注意,在您的示例數據中,日期在第一個表格和第二個表格之間切換。

1

如果你只想檢查1日期,這將工作。你應該能夠適應這個樣本來滿足你的需求。

SELECT c.CreateDateUTC, DATEPART(MONTH, c.CreateDateUTC) 'MONTH', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 1 THEN 1 
    END 'JAN', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 2 THEN 1 
    END 'FEB', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 3 THEN 1 
    END 'MAR', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 4 THEN 1 
    END 'APR', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 5 THEN 1 
    END 'MAY', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 6 THEN 1 
    END 'JUN', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 7 THEN 1 
    END 'JUL', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 8 THEN 1 
    END 'AUG', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 9 THEN 1 
    END 'SEP', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 10 THEN 1 
    END 'OCT', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 11 THEN 1 
    END 'NOV', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 12 THEN 1 
    END 'DEC' 
FROM dbo.Code c 

結果:

enter image description here

0

擴大,請務必檢查空,並且可以使用ISNULL(起始日期,GETDATE())今天會給你,如果適合你的範圍需要。

select *, 
    case when StartDate is not null and EndDate is not null and 1 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jan, 
    case when StartDate is not null and EndDate is not null and 2 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Feb, 
    case when StartDate is not null and EndDate is not null and 3 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Mar, 
    case when StartDate is not null and EndDate is not null and 4 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Apr, 
    case when StartDate is not null and EndDate is not null and 5 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end May, 
    case when StartDate is not null and EndDate is not null and 6 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jun, 
    case when StartDate is not null and EndDate is not null and 7 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jul, 
    case when StartDate is not null and EndDate is not null and 8 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Aug, 
    case when StartDate is not null and EndDate is not null and 9 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Sep, 
    case when StartDate is not null and EndDate is not null and 10 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Oct, 
    case when StartDate is not null and EndDate is not null and 11 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Nov, 
    case when StartDate is not null and EndDate is not null and 12 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Dec 
from Foo 
+0

IS NOT NULL檢查在這裏是多餘的。它們不會被評估爲值爲NULL的日期之間。 –

相關問題