我有这种格式的数据:
Prod Name Qty
-------------------
Ink Joe 100
Pen Joe 10
Ink Jay 50
Pen Jay 5我想编写一个SQL查询,它返回以下格式的数据:
Prod Joe Jay
---------------------
Ink 100 50
Pen 10 5请注意,Prod和Name都是动态的( Prod条目和Name条目的数量不断变化),感谢您在这方面的帮助。
发布于 2019-02-04 23:25:46
使用条件聚合:
select prod,
max(case name when 'Joe' then Qty end) Joe,
max(case name when 'Jay' then Qty end) Jay
from tablename
group by prod请参阅demo
https://stackoverflow.com/questions/54518935
复制相似问题