将多行转换为列w。单排

时间:2015-08-03 12:30:45

标签: sql sql-server-2008 tsql

我需要一些帮助来确定如何最好地将数组转换为行向量。我的数组看起来像这样:

+-----+-------+----------+
| ID  | Grade | Quantity |
+-----+-------+----------+
| Ape | Water | Y        |
| Ape | Juice | Y        |
| Ape | Milk  | Y        |
+-----+-------+----------+

每个ID最多可以有4行,仅按等级(水,果汁,啤酒,牛奶);可能值列表是静态的。

我想要的输出是:

+-----+----------+-------+-------+------+------+
| ID  | Quantity | Water | Juice | Beer | Milk |
+-----+----------+-------+-------+------+------+
| Ape | Y        |     1 |     1 |    0 |    1 |
+-----+----------+-------+-------+------+------+

我自己的努力把我带到PIVOT运算符,它将Grade-values转换为列,但它没有按行对行进行分组,在转换后留下了相同数量的行。

SELECT ID, Quantity, Water, Juice, Beer, Milk
FROM
    (SELECT ID, Grade, Quantity FROM Feeding WHERE ID = 'Ape') src
    PIVOT(
    COUNT(Quantity) FOR [Grade] IN (ID, Quantity, Water, Juice, Beer, Milk) 
    )AS TransformData

输出:

+-----+----------+-------+-------+------+------+
| ID  | Quantity | Water | Juice | Beer | Milk |
+-----+----------+-------+-------+------+------+
| Ape | Y        |     1 |     0 |    0 |    0 |
| Ape | Y        |     0 |     1 |    0 |    0 |
| Ape | Y        |     0 |     0 |    0 |    1 |
+-----+----------+-------+-------+------+------+

有什么建议吗?

5 个答案:

答案 0 :(得分:1)

怎么样;

;WITH Feeding(id,grade,quantity) as (
    select 'Ape','Water','Y' union all
    select 'Ape','Juice','Y' union all
    select 'Ape','Juice','Y' union all
    select 'Ape','Juice','Y' union all
    select 'Ape','Juice','Y' union all
    select 'Ape','Milk', 'N' 
)

SELECT * FROM
    (SELECT ID, Grade, Quantity agg, Quantity FROM Feeding WHERE ID = 'Ape') src
    PIVOT ( COUNT(agg) FOR [Grade] IN (Water, Juice, Beer, Milk) ) AS TransformData

-

ID  Quantity    Water   Juice   Beer    Milk
Ape N           0       0       0       1
Ape Y           1       4       0       0

答案 1 :(得分:0)

您可以尝试以下查询: -

SELECT ID, Quantity, CASE WHEN Grade = 'WATER' THEN 1 ELSE 0 END AS WATER,
                     CASE WHEN Grade = 'JUICE' THEN 1 ELSE 0 END AS JUICE,
                     CASE WHEN Grade = 'BEER' THEN 1 ELSE 0 END AS BEER,
                     CASE WHEN Grade = 'MILK' THEN 1 ELSE 0 END AS MILK
FROM YOUR_TABLE;

答案 2 :(得分:0)

select id, quantity,
case when grade = 'Water' then 1 else 0 end as Water,
when grade = 'Juice' then 1 else 0 end as Juice,
when grade = 'Milk' then 1 else 0 end as Milk,
when grade = 'Beer' then 1 else 0 end as Beer
from feeding  

由于值列表是静态的,因此这是一种方法。

答案 3 :(得分:0)

试试这个

SELECT id
    ,quantity
    ,CASE 
        WHEN grade = 'Water'
            THEN 1
        ELSE 0
        END AS Water
    ,when grade = 'Juice' then 1 ELSE 0 END AS Juice
    ,when grade = 'Milk' then 1 ELSE 0 END AS Milk
    ,when grade = 'Beer' then 1 ELSE 0 END AS Beer
FROM feeding

答案 4 :(得分:0)

尝试此操作以获得单行结果

SELECT Id,Quantity,
       SUM(CASE WHEN Grade='Water' THEN 1 ELSE 0 END) AS Water,
       SUM(CASE WHEN Grade='Juice ' THEN 1 ELSE 0 END) AS Juice ,
       SUM(CASE WHEN Grade='Beer' THEN 1 ELSE 0 END) AS Beer,
       SUM(CASE WHEN Grade='Milk' THEN 1 ELSE 0 END) AS Milk
FROM Feed F 
GROUP BY Id,Quantity

SQL Fiddle Demo