1

我在这个结构的表中有数据:

Region | Date       | Value

A      | 01/01/2014 |    100

A      | 01/20/2014 |     50

A      | 01/02/2014 |    200

A      | 01/05/2014 |    300

B      | 01/01/2014 |    50

B      | 02/15/2014 |    70

B      | 02/25/2014 |    50

C      | 05/02/2014 |    70

我正在尝试使用 T-SQL 查询创建这样的数据透视视图:

Region | Jan-2014 | Feb-2014 | Mar-014 | Apr-2014 | May-2014 | -> thru desired month-year

A      |    150   |   200    |   0     |     0    |    300   |

B      |     50   |   120    |   0     |     0    |      0   |

C      |      0   |     0    |   0     |     0    |     70   |

请注意,需要汇总给定区域同一月份的多个值 没有记录的月份仍应显示为具有零值的列(例如:三月和四月)

我厌倦了使用枢轴选项,withroll up 等,但似乎无法让它发挥作用,非常感谢任何帮助..

谢谢你。

4

1 回答 1

3

您没有这些月份的结果的原因是您缺少要转入列​​的日期。

有几种方法可以做到这一点。IN您可以对查询部分中的所有日期值进行硬编码,以便显示列:

select Region,
  isnull([Jan-2014], 0) [Jan-2014], isnull([Feb-2014], 0) [Feb-2014], 
  isnull([Mar-2014], 0) [Mar-2014], isnull([Apr-2014], 0) [Apr-2014],
  isnull([May-2014], 0) [May-2014], isnull([Jun-2014], 0) [Jun-2014], 
  isnull([Jul-2014], 0) [Jul-2014], isnull([Aug-2014], 0) [Aug-2014],
  isnull([Sep-2014], 0) [Sep-2014], isnull([Oct-2014], 0) [Oct-2014], 
  isnull([Nov-2014], 0) [Nov-2014], isnull([Dec-2014], 0) [Dec-2014]
from 
(
  select left(datename(month, t.date), 3) +'-'
            + cast(year(t.date) as char(4)) monthYear,
    t.region,
    t.value
  from yt t
) src
pivot
(
  sum(value)
  for monthYear in ([Jan-2014], [Feb-2014], [Mar-2014], [Apr-2014],
                    [May-2014], [Jun-2014], [Jul-2014], [Aug-2014],
                    [Sep-2014], [Oct-2014], [Nov-2014], [Dec-2014])
) piv;

请参阅SQL Fiddle with Demo

另一种方法是创建一个可以在查询中使用的日期表。创建后,您可以使用 aLEFT JOIN到您的表中,以便返回您想要显示的所有日期。您可以创建递归查询以在 PIVOT 查询中生成此列表,也可以使用日期列表填充表。递归查询将与此类似:

;with dates (startDate, endDate) as 
(
  select min(date), cast('2014-12-31' as date)
  from yt
  union all
  select dateadd(m, 1, startDate), enddate
  from dates
  where month(startDate) + 1 <= month(enddate)
)
select startDate
from dates;

请参阅SQL Fiddle with Demo。如果您将其加入您的 PIVOT,则查询将是:

select *
from 
(
  select left(datename(month, d.startdate), 3) +'-'
            + cast(year(d.startdate) as char(4)) monthYear,
    t.region,
    t.value
  from dates d
  left join yt t
    on month(d.startdate) = month(t.date)
    and year(d.startdate) = year(t.date)
) src
pivot
(
  sum(value)
  for monthYear in ([Jan-2014], [Feb-2014], [Mar-2014], [Apr-2014],
                    [May-2014], [Jun-2014], [Jul-2014], [Aug-2014],
                    [Sep-2014], [Oct-2014], [Nov-2014], [Dec-2014])
) piv
where region is not null;

请参阅SQL Fiddle with Demo。您可以将其用作 PIVOT 查询的一部分,但您仍需要手动编码INPIVOT 子句中的所有日期。

我猜你想使用这个查询的动态 SQL 版本,所以日期会根据你的需要而改变。动态版本将是:

DECLARE @cols AS NVARCHAR(MAX),
    @colsNull AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @startdate datetime = '2014-01-01',
    @enddate datetime = '2014-12-01'

;with dates (startDate, endDate) as 
(
  select @startdate, @enddate
  from yt
  union all
  select dateadd(m, 1, startDate), enddate
  from dates
  where dateadd(m, 1, startDate) <= enddate
)
select @cols = STUFF((SELECT ',' + QUOTENAME(left(datename(month, d.startdate), 3) +'-'
                                                      + cast(year(d.startdate) as char(4))) 
                    from dates d
                    -- where startdate >= '2014-01-01' and startdate <= '2014-06-01'
                    group by d.startdate
                    order by d.startdate
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,''),
    @colsNull = STUFF((SELECT ', isnull(' + QUOTENAME(left(datename(month, d.startdate), 3) +'-'
                                                      + cast(year(d.startdate) as char(4)))+', 0) as '+QUOTENAME(left(datename(month, d.startdate), 3) +'-'
                                                      + cast(year(d.startdate) as char(4)))
                    from dates d
                    -- where startdate >= '2014-01-01' and startdate <= '2014-06-01'
                    group by d.startdate
                    order by d.startdate
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
from dates

set @query = 'SELECT region, ' + @colsNull + ' from 
             (
                  select left(datename(month, t.date), 3) +''-''
                            + cast(year(t.date) as char(4)) monthYear,
                    t.region,
                    t.value
                  from yt t
            ) x
            pivot 
            (
                sum(value)
                for monthyear in (' + @cols + ')
            ) p '

execute(@query);

请参阅SQL Fiddle with Demo。此版本使用递归 CTE 生成将在动态 sql 字符串中使用的日期列表。即使您的数据表可能不存在显示的月份,您仍然会有一个新列。

这给出了一个结果:

| REGION | JAN-2014 | FEB-2014 | MAR-2014 | APR-2014 | MAY-2014 | JUN-2014 | JUL-2014 | AUG-2014 | SEP-2014 | OCT-2014 | NOV-2014 | DEC-2014 |
----------------------------------------------------------------------------------------------------------------------------------------------
|      A |      650 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |
|      B |       50 |      120 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |
|      C |        0 |        0 |        0 |        0 |       70 |        0 |        0 |        0 |        0 |        0 |        0 |        0 |
于 2013-05-09T11:50:53.757 回答