SQL - 可能的数据透视问题

时间:2015-11-03 21:18:24

标签: sql sql-server pivot

我有一个包含以下结构的表

Item Id, Start Date, End Date  
1      , 2015-01-01, 2015-06-01  
2      , 2015-01-01, 2015-02-01   
3      , 2015-03-01, 2015-08-01  
4      , 2015-06-01, 2015-10-01

我想查看结果,所以每个月我都会

每一行都将包含本月内项目的 ID。

实施例:   我要求2015-01-012015-03-01内的所有项目。

结果应以列的形式显示该范围内的所有月份。因此,在这种情况下,它是3列,Jan FebMarch

行数将是该范围内的项目总数。但是,只有当该项目在范围内时,每个单元格才应显示项目ID的值:

示例:

2015-01-01, 2015-02-01, 2015-03-01   
    1           1          1  
    2           2         NULL  
   NULL        NULL        3

3 个答案:

答案 0 :(得分:1)

为了使用pivot,您可以创建一个递归cte获取每个项目ID及其涵盖的月份列表,然后转动cte。

;WITH cte AS 
(
    SELECT [Item Id], [Start Date], [End Date] 
    FROM   Table1
    WHERE  [Start Date] BETWEEN '2015-01-01' AND '2015-03-01' --Date Range you want
           OR [End Date] BETWEEN '2015-01-01' AND '2015-03-01' --Date Range you want
    UNION  ALL
    SELECT [Item Id], DATEADD(MONTH, 1, [Start Date]), [End Date]
    FROM   cte
    WHERE  DATEADD(MONTH, 1, [Start Date]) <= [End Date]
)
SELECT [2015-01-01],[2015-02-01],[2015-03-01] --List of Dates you want
FROM (
    SELECT [Item Id] rn, -- need a unique id here to give one row per record
           [Item Id], 
           CONVERT(VARCHAR(10), [Start Date], 120) [Start Date] -- Format date to yyyy-mm-dd
    FROM   cte
) t
PIVOT 
( MAX([Item Id]) 
    FOR [Start Date] IN ([2015-01-01],[2015-02-01],[2015-03-01])
) p

答案 1 :(得分:1)

您很可能需要使用动态SQL。

  

这是您的数据:

declare @first date = '20150101';
declare @last date = '20150301';

Create Table #items(ItemId int, StartDate date, EndDate date);
Insert into #items(ItemId, StartDate, EndDate) values
(1, '2015-01-01', '2015-06-01')  
, (2, '2015-01-01', '2015-02-01')   
, (3, '2015-03-01', '2015-08-01')  
, (4, '2015-06-01', '2015-10-01');
  

首先需要获取值和列的范围:

declare @values varchar(max);
declare @cols varchar(max);

with range(d) as (
    Select top(DATEDIFF(month, @first, @last)+1) cast(DATEADD(month, ROW_NUMBER() over(order by (select 0))-1, @first) as varchar(20))
    From (
        Select 1 From (values(1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) as x1(n)
        Cross Join (values(1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) as x2(n)
    ) as x(n)
)
Select @values = coalesce(''+@values+ ', ', ' ') + '('''+d+''')' 
    , @cols = coalesce(''+@cols+ ', ', ' ') + '['+left(DATENAME(month, d), 3)+CAST(year(d) as char(4))+']' 
From range
;

这基本上为@first和@last之间的每个日期创建一行,并用括号和逗号(@values)或括号(@cols)连接它们。

  

@values和@cols中的内容如下所示:

 @values = ('2015-01-01'), ('2015-02-01'), ('2015-03-01')
 @cols = [Jan2015], [Feb2015], [Mar2015]
  

然后使用这两个变量创建一个SQL脚本:

declare @sql nvarchar(max);

Set @sql = '
Select * 
From (
    Select i.ItemId, d = left(DATENAME(month, r.d), 3)+CAST(year(r.d) as char(4))
        , id = case when r.d >= i.StartDate and r.d <= i.EndDate then i.ItemId end
    From (values'+@values+') as r(d) 
    Cross Join (Select ItemId, StartDate, EndDate From #items 
        Where (@first >= StartDate and @first <= EndDate) or (@last >= StartDate and @last <= EndDate)
    ) i 
) as dates
Pivot (
    min(id)
    For d in('+@cols+')
) as piv
';

这是透视查询。

  

在此示例中,创建的SQL将如下所示:

Select * 
From (
    Select i.ItemId, d = left(DATENAME(month, r.d), 3)+CAST(year(r.d) as char(4))
        , id = case when r.d >= i.StartDate and r.d <= i.EndDate then i.ItemId end
    From (values ('2015-01-01'), ('2015-02-01'), ('2015-03-01')) as r(d) 
    Cross Join (Select ItemId, StartDate, EndDate From #items 
        Where (@first >= StartDate and @first <= EndDate) or (@last >= StartDate and @last <= EndDate)
    ) i 
) as dates
Pivot (
    min(id)
    For d in( [Jan2015], [Feb2015], [Mar2015])
) as piv
  

您最终可以执行脚本:

exec sp_executesql @sql, N'@first date, @last date', @first, @last;
  

输出继电器:

ItemId  Jan2015 Feb2015 Mar2015
1       1       1       1
2       2       2       NULL
3       NULL    NULL    3

答案 2 :(得分:0)

可能类似....

scala> val regexpr = "[A-Za-z]+".r