使用pivot在sql server中的列中显示多个值

时间:2013-01-31 14:19:57

标签: sql-server sql-server-2008-r2

declare @t table 
(
    driver varchar(20),
    roadband varchar(20),
    category varchar(20),
    points int

)   

INSERT INTO @T VALUES( 'Dan'    ,'20 Mph','CAT1',58)
INSERT INTO @T VALUES( 'Dan'    ,'20 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'Dan'    ,'20 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'Dan'    ,'30 Mph',  'CAT1', 102)
INSERT INTO @T VALUES( 'Dan'    ,'30 Mph',  'CAT2', 30)
INSERT INTO @T VALUES( 'Dan'    ,'30 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'Dan'    ,'40 Mph',  'CAT1', 6)
INSERT INTO @T VALUES( 'Dan'    ,'40 Mph',  'CAT2', 3)
INSERT INTO @T VALUES( 'Dan'    ,'40 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'Dan'    ,'50 Mph',  'CAT1', 5)
INSERT INTO @T VALUES( 'Dan'    ,'50 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'Dan'    ,'50 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'Dan'    ,'60 Mph',  'CAT1', 0)
INSERT INTO @T VALUES( 'Dan'    ,'60 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'Dan'    ,'60 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'Dan'    ,'70 Mph',  'CAT1', 0)
INSERT INTO @T VALUES( 'Dan'    ,'70 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'Dan'    ,'70 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'John'   ,'20 Mph',  'CAT1', 6)
INSERT INTO @T VALUES( 'John'   ,'20 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'John'   ,'20 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'John'   ,'30 Mph',  'CAT1', 1228)
INSERT INTO @T VALUES( 'John'   ,'30 Mph',  'CAT2', 654)
INSERT INTO @T VALUES( 'John'   ,'30 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'John'   ,'40 Mph',  'CAT1', 196)
INSERT INTO @T VALUES( 'John'   ,'40 Mph',  'CAT2', 117)
INSERT INTO @T VALUES( 'John'   ,'40 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'John'   ,'50 Mph',  'CAT1', 6)
INSERT INTO @T VALUES( 'John'   ,'50 Mph',  'CAT2', 14)
INSERT INTO @T VALUES( 'John'   ,'50 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'John'   ,'60 Mph',  'CAT1', 0)
INSERT INTO @T VALUES( 'John'   ,'60 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'John'   ,'60 Mph',  'CAT3', 0)
INSERT INTO @T VALUES( 'John'   ,'70 Mph',  'CAT1', 0)
INSERT INTO @T VALUES( 'John'   ,'70 Mph',  'CAT2', 0)
INSERT INTO @T VALUES( 'John'   ,'70 Mph',  'CAT3', 0)

我需要转向RoadbandCategory ..

我写了查询..需要一些帮助..

select * from 
(
   select 
     Driver,  ROADBAND,  category,SUM(points) as Points
   from @t
   where DRIVER is not null
   group by DRIVER ,ROADBAND,CATEGORY
) main
pivot
(
    sum(Points)
    for ROADBAND in ([20 Mph],[30 Mph],[40 Mph],[50 Mph],[60 Mph],[70 Mph])
) pq

现在如何通过公路带为每个类别制作枢轴。

请帮忙。

1 个答案:

答案 0 :(得分:0)

根据你的问题,我不确定你想要的列中的值和行中应该是什么,所以我写了两个查询,希望其中一个回答你的问题:

SELECT  *
FROM    (
            SELECT  category
                    , roadband
                    , SUM(points) points_per_category_roadband
            FROM    @t
            GROUP BY
                    category, roadband
) tbl
PIVOT
(
    SUM(points_per_category_roadband) FOR roadband IN ([20 Mph],[30 Mph],[40 Mph],[50 Mph],[60 Mph],[70 Mph])
) pvt


SELECT  *
FROM    (
            SELECT  roadband
                    , category
                    , SUM(points) points_per_category_roadband
            FROM    @t
            GROUP BY
                    roadband, category
) tbl
PIVOT
(
    SUM(points_per_category_roadband) FOR category IN ([CAT1],[CAT2],[CAT3])
) pvt