0

我有一个存储数据的表,如下所示:
username status
user1 HT
user2 EC
user1 PR
user1 EC
user2 HT

我需要这样的结果

用户名 HT EC PR GrandTotal
user1 1 1 1 3
user2 1 1 0 2
Total 2 2 1 5

请任何人帮助我。提前致谢

4

2 回答 2

1

当状态值固定时,这很简单。

select 
    username,
    sum(case when status='HT' then 1 else 0 end) as HT,
    sum(case when status='EC' then 1 else 0 end) as EC,
    sum(case when status='PR' then 1 else 0 end) as PR,
    count(*) as total
from
    table
group by
    username
WITH ROLLUP

如果它们是动态的,则需要使用http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

于 2012-07-16T06:36:43.407 回答
1
        USE AdventureWorks
    GO
    -- Creating Test Table
    CREATE TABLE Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)
    GO
    -- Inserting Data into Table
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('KATE','VEG',2)
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('KATE','SODA',6)
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('KATE','MILK',1)
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('KATE','BEER',12)
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('FRED','MILK',3)
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('FRED','BEER',24)
    INSERT INTO Product(Cust, Product, QTY)
    VALUES('KATE','VEG',3)
    GO
    -- Selecting and checking entires in table
    SELECT *
    FROM Product
    GO
    -- Pivot Table ordered by PRODUCT
    SELECT PRODUCT, FRED, KATE
    FROM (
    SELECT CUST, PRODUCT, QTY
    FROM Product) up
    PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt
    ORDER BY PRODUCT
    GO
    -- Pivot Table ordered by CUST
    SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
    FROM (
    SELECT CUST, PRODUCT, QTY
    FROM Product) up
    PIVOT (SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt
    ORDER BY CUST
    GO
    -- Unpivot Table ordered by CUST
    SELECT CUST, PRODUCT, QTY
    FROM
    (
    SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
    FROM (
    SELECT CUST, PRODUCT, QTY
    FROM Product) up
    PIVOT
    ( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt) p
    UNPIVOT
    (QTY FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)
    ) AS Unpvt
    GO
    -- Clean up database
    DROP TABLE Product
    GO

    ResultSet:
    -- Selecting and checking entires in table
    Cust Product QTY
    ------------------------- -------------------- -----------
    KATE VEG 2
    KATE SODA 6
    KATE MILK 1
    KATE BEER 12
    FRED MILK 3
    FRED BEER 24
    KATE VEG 3

    -- Pivot Table ordered by PRODUCT
    PRODUCT FRED KATE
    -------------------- ----------- -----------
    BEER 24 12
    MILK 3 1
    SODA NULL 6
    VEG NULL 5

    -- Pivot Table ordered by CUST
    CUST VEG SODA MILK BEER CHIPS
    ------------------------- ----------- ----------- ----------- ----------- -----------
    FRED NULL NULL 3 24 NULL
    KATE 5 6 1 12 NULL

    -- Unpivot Table ordered by CUST
    CUST PRODUCT QTY
    ------------------------- -------- -----------
    FRED MILK 3
    FRED BEER 24
    KATE VEG 5
    KATE SODA 6
    KATE MILK 1
    KATE BEER 12 12

参考:http: //blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

于 2012-07-16T06:37:22.073 回答