0

我有下表

Name    Month   Year    Count
----------------------------
xxx     12      2012    24
xxx     1       2013    42
xxx     2       2013    23
yyy     12      2012    34
yyy     1       2013    12
yyy     2       2013    54

我想把它转换成下面的格式,

Name    Dec-12  Jan-13  Feb-13
--------------------------------
xxx     24      42      23
yyy     34      12      54

如何应用支点?

4

3 回答 3

3

由于您使用的是 SQL Server,因此您可以通过多种方式将数据从行转换为列。

如果您的值是有限的或者您有已知数量的值,那么您可以使用静态枢轴对值进行硬编码:

select name, [Dec_12], [Jan_13], [Feb_13]
from
(
  select name,
    left(datename(month, dateadd(month, month, 0) -1), 3) +'_'+right(cast(year as varchar(4)), 2) MY,
    [count]
  from yourtable
) src
pivot
(
  sum(count)
  for my in ([Dec_12], [Jan_13], [Feb_13])
) piv;

请参阅SQL Fiddle with Demo

现在,如果您有未知数量的值,那么您将需要实现动态 SQL 来生成结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(my) 
                    from 
                    (
                      select left(datename(month, dateadd(month, month, 0) -1), 3) +'_'+right(cast(year as varchar(4)), 2) my,
                         CAST(
                            CAST(year AS VARCHAR(4)) +
                            RIGHT('0' + CAST(month AS VARCHAR(2)), 2) +
                            '01'
                         AS DATETIME) fulldate
                      from yourtable
                    ) t
                    group by my, fulldate
                    order by fulldate 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT name, ' + @cols + ' 
              from 
             (
                select name,
                  left(datename(month, dateadd(month, month, 0) -1), 3) +''_''+right(cast(year as varchar(4)), 2) MY,
                  [count]
                from yourtable
            ) x
            pivot 
            (
                sum(count)
                for my in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demo

与此版本和静态版本的区别在于,如果您需要未知数量的日期,或者希望在新日期可用时自动更新,这将在不更改代码的情况下返回新数据。

两个查询的结果是:

| NAME | DEC_12 | JAN_13 | FEB_13 |
-----------------------------------
|  xxx |     24 |     42 |     23 |
|  yyy |     34 |     12 |     54 |
于 2013-03-21T10:10:52.867 回答
1

尝试这个:

WITH CTE
AS
(
  SELECT
    Name, 
    CAST(Month AS VARCHAR(2)) + '-' + CAST(Year AS VARCHAR(4)) AS MonthYear,
    [Count]
  FROM tablename
)
SELECT 
  Name,
  [12-2012] AS 'Dec-12', 
  [1-2013]  AS 'Jan-13', 
  [2-2013]  AS 'Feb-13'
FROM CTE
PIVOT
(  
   MAX([Count])
   FOR MonthYear IN([12-2012], 
                    [1-2013], 
                    [2-2013])
) AS p;

SQL 小提琴演示

于 2013-03-21T07:39:05.600 回答
1
  SELECT t.name
         , MAX(CASE
                 WHEN t.month=12 AND t.year = 2012
                   THEN count
                 ELSE NULL
               END) AS "Dec_12"
         , MAX(CASE
                 WHEN t.month=1 AND t.year = 2013
                   THEN count
                 ELSE NULL
               END) AS "Jan_13"
         , MAX(CASE
                 WHEN t.month=2 AND t.year = 2013
                   THEN count
                 ELSE NULL
               END) AS "Feb_13"
    FROM table t
GROUP BY t.name
;
于 2013-03-21T07:44:38.480 回答