SQL Server - 行到列(分组)

时间:2014-03-28 00:58:41

标签: sql sql-server tsql pivot rows

我已经尝试使用PIVOT将我的一些行组合成列,但问题是,我必须将它们分组,我无法弄清楚我该怎么做。

SQL查询:

select  res.tipo,
            it.itemname as item,
            sum(resi.quantity) as qt
from    Reserve as res inner join
            ReserveItems as resi on res.id_reserve = resi.id_reserve inner join
            Items as it on resi.defindex = it.defindex
where   res.situacao = 3
group by res.tipo, it.id, it.itemname
order by tipo, it.id

结果:

tipo   item                                                                                                 qt
------ ---------------------------------------------------------------------------------------------------- -----------
0      Mann Co. Supply Crate Key                                                                            6
0      Tour of Duty Ticket                                                                                  10
0      Reinforced Robot Emotion Detector                                                                    5
0      Reinforced Robot Bomb Stabilizer                                                                     1
0      Battle-Worn Robot Taunt Processor                                                                    3
0      Battle-Worn Robot KB-808                                                                             22
0      Battle-Worn Robot Money Furnace                                                                      19
1      Mann Co. Supply Crate Key                                                                            41
1      Tour of Duty Ticket                                                                                  31
1      Pristine Robot Currency Digestor                                                                     1
1      Pristine Robot Brainstorm Bulb                                                                       2
1      Reinforced Robot Emotion Detector                                                                    32
1      Reinforced Robot Humor Supression Pump                                                               45
1      Reinforced Robot Bomb Stabilizer                                                                     39
1      Battle-Worn Robot Taunt Processor                                                                    69
1      Battle-Worn Robot KB-808                                                                             78
1      Battle-Worn Robot Money Furnace                                                                      109

期望的结果:

item                                        qt_1        qt_0
------------------------------------------  ----------  --------
Mann Co. Supply Crate Key                   41          6
Tour of Duty Ticket                         27          6
Pristine Robot Currency Digestor            1           0
Pristine Robot Brainstorm Bulb              2           0
Reinforced Robot Emotion Detector           32          5
Reinforced Robot Humor Supression Pump      45          0
Reinforced Robot Bomb Stabilizer            39          1
Battle-Worn Robot Taunt Processor           89          3
Battle-Worn Robot KB-808                    92          16
Battle-Worn Robot Money Furnace             109         19

有没有可能以简单的方式做到这一点? (不使用#temp,插入/更新值)。使用pivot对我来说是最好的=)

编辑:

解决方案基于JanR的回答。再次感谢,伙计!

select  it.itemname as item,
            sum(case when res.tipo = 0 then resi.quantity else 0 end) as qt_compra,
            sum(case when res.tipo = 1 then resi.quantity else 0 end) as qt_venda
from    Reserve as res inner join
            ReserveItems as resi on res.id_reserve = resi.id_reserve inner join
            Items as it on resi.defindex = it.defindex
where   res.situacao = 3
group by it.id, it.itemname
order by it.id

1 个答案:

答案 0 :(得分:1)

不确定这是否会回答您的问题,但我发现您将tipo包含在您的group by子句中,这将导致记录被拆分,例如'Mann Co. Supply Crate Key'可以根据您的信息,tipo为0或1。这会将它们分组为0 Mann Co. Supply Crate Key&第二组1 Mann Co. Supply Crate Key

编辑:查看您的查询,您可能需要这样的内容:

select  
            it.itemname as item,
            sum(case when resi.tipo = 0 then resi.quantity else 1 end) as qty_0,
            sum(case when resi.tipo = 1 then resi.quantity else 0 end) as qty_1
from    Reserve as res inner join
            ReserveItems as resi on res.id_reserve = resi.id_reserve inner join
            Items as it on resi.defindex = it.defindex
where   res.situacao = 3
group by it.itemname
order by tipo, it.id

请记住,如果不知道表格结构,就会有点困难:)