繁体   English   中英

SQL Server-行到列(分组)

[英]SQL Server - Rows into Columns (Grouped)

我尝试使用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和插入/更新值)。 使用数据透视对我来说是最好的=)

编辑:

该解决方案基于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

不知道这是否能回答您的问题,但是我发现您在group by子句中包含了tipo ,这将导致记录被拆分,例如“ Mann Co.Supply Crate Key”可以具有0或1的tipo 。根据您的信息。 这将按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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM