2017-08-02 66 views
0

我有一個表,這個數據:動態列

FirstDiscovered|LastDiscovered|Application|Computer|Organisation 
---------------------------------------------------------------- 
05.03.2017  |05.08.2017 |App A  |Comp 1 |Org 1 
---------------------------------------------------------------- 
04.13.2017  |08.01.2017 |App A  |Comp 2 |Org 1 
---------------------------------------------------------------- 
05.01.2017  |08.01.2017 |App B  |Comp 2 |Org 1 
---------------------------------------------------------------- 
06.13.2017  |08.02.2017 |App A  |Comp 3 |Org 2 
---------------------------------------------------------------- 
07.14.2017  |08.02.2017 |App A  |Comp 4 |Org 2 
---------------------------------------------------------------- 
07.14.2017  |08.02.2017 |App B  |Comp 3 |Org 2 
---------------------------------------------------------------- 

我想與基於日期屬性「firstDiscovered」和「lastDiscovered」 select語句中的動態列延伸。 假設有一行firstDiscovered = 05.27.17和lastDiscovered = 06.25.17。那麼我預計5月17日和6月17日會有兩個新專欄填入「1」。通過設置爲「0」或空的其他行產生的所有其他列(個月),見例如:

FirstDiscovered|LastDiscovered|Application|Computer|Organisation|Apr 17|May 17|Jun 17|Jul 17|Aug 17 
--------------------------------------------------------------------------------------------------- 
05.03.2017  |05.08.2017 |App A  |Comp 1 |Org 1  |0  |1  |0  |0  |0 
--------------------------------------------------------------------------------------------------- 
04.13.2017  |08.01.2017 |App A  |Comp 2 |Org 1  |1  |1  |1  |1  |1 
--------------------------------------------------------------------------------------------------- 
05.01.2017  |08.02.2017 |App B  |Comp 2 |Org 1  |0  |1  |1  |1  |1 
--------------------------------------------------------------------------------------------------- 
06.13.2017  |08.02.2017 |App A  |Comp 3 |Org 2  |0  |0  |1  |1  |1 
--------------------------------------------------------------------------------------------------- 
07.14.2017  |08.02.2017 |App A  |Comp 4 |Org 2  |0  |0  |0  |1  |1 
--------------------------------------------------------------------------------------------------- 
07.18.2017  |08.02.2017 |App B  |Comp 3 |Org 2  |0  |0  |0  |1  |1 
--------------------------------------------------------------------------------------------------- 

將是巨大的,如果有人能幫助我。

+1

有關使用CASE什麼基於表達式添加「動態」列? – mauro

+0

我的猜測你想要這個動態添加開始月份和結束月份之間的所有列,並使其動態?這是可行的,但在SSRS的交叉標籤報告中更容易。 – KeithL

+0

必須是選擇語句。無法使用SSRS –

回答

0

可以如下查詢:

select * from #DiscoverData d 
cross apply 
(Select top (datediff(mm,d.firstdiscovered, d.lastdiscovered) +1) 
    RowN = Right(Convert(varchar(10), Dateadd(mm, Row_Number() over(order by (Select NULL))-1 , d.FirstDiscovered),6),6)   
    from master..spt_values s1, master..spt_values s2) c 
pivot (Count(RowN) for RowN in ([Apr 17],[May 17],[Jun 17],[Jul 17], [Aug 17])) p 

幾個月來,你可以使用查詢,如下的動態列表:

Declare @cols1 varchar(max) 
Declare @query nvarchar(max) 

Select @cols1 = stuff((
    Select ','+QuoteName(RowN) from (Select min(firstdiscovered) as firstdiscovered, max(lastdiscovered) as lastdiscovered from #DiscoverData) d 
    cross apply 
    (Select top (datediff(mm,d.firstdiscovered, d.lastdiscovered) +1) 
     RowN = Right(Convert(varchar(10), Dateadd(mm, Row_Number() over(order by (Select NULL))-1 , d.FirstDiscovered),6),6)   
     from master..spt_values s1, master..spt_values s2) c 
    for xml path('')),1,1,'') 

Set @query = ' Select * from #DiscoverData d 
    cross apply 
    (Select top (datediff(mm,d.firstdiscovered, d.lastdiscovered) +1) 
     RowN = Right(Convert(varchar(10), Dateadd(mm, Row_Number() over(order by (Select NULL))-1 , d.FirstDiscovered),6),6)   
     from master..spt_values s1, master..spt_values s2) c 
    pivot (Count(RowN) for RowN in (' + @cols1 + ')) p ' 

Exec sp_executesql @query 

輸出如下:


+-----------------+----------------+-------------+----------+--------------+--------+--------+--------+--------+--------+ 
| FirstDiscovered | lastdiscovered | Application | computer | Organisation | Apr 17 | May 17 | Jun 17 | Jul 17 | Aug 17 | 
+-----------------+----------------+-------------+----------+--------------+--------+--------+--------+--------+--------+ 
| 2017-04-13  | 2017-08-01  | App A  | Comp 2 | Org 1  |  1 |  1 |  1 |  1 |  1 | 
| 2017-05-01  | 2017-08-01  | App B  | Comp 2 | Org 1  |  0 |  1 |  1 |  1 |  1 | 
| 2017-05-03  | 2017-05-08  | App A  | Comp 1 | Org 1  |  0 |  1 |  0 |  0 |  0 | 
| 2017-06-13  | 2017-08-02  | App A  | Comp 3 | Org 2  |  0 |  0 |  1 |  1 |  1 | 
| 2017-07-14  | 2017-08-02  | App A  | Comp 4 | Org 2  |  0 |  0 |  0 |  1 |  1 | 
| 2017-07-14  | 2017-08-02  | App B  | Comp 3 | Org 2  |  0 |  0 |  0 |  1 |  1 | 
+-----------------+----------------+-------------+----------+--------------+--------+--------+--------+--------+--------+ 
+0

動態月查詢一般運行。 但我得到消息「TOP子句包含一個無效值。」 結果不顯示所有數據 –

+0

這意味着您在首次發現和上次發現之間的差異中有一些負面數據。我們在該場景中需要做什麼? –