0
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

现在如何通过路带对每个类别进行支点。

请帮忙。

4

1 回答 1

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
于 2013-01-31T20:10:21.247 回答