基于日期的动态列

时间:2017-08-02 15:28:25

标签: sql sql-server pivot

我有一张包含这些数据的表格:

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 个答案:

答案 0 :(得分: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 |
+-----------------+----------------+-------------+----------+--------------+--------+--------+--------+--------+--------+