表1
id no name value
001 grid1 rajan 200
001 grid2 rajan 300
002 grid1 mahesh 100
002 grid2 mahesh 200
003 grid1 jayan 200
003 grid2 jayan 50
我想找到每个id的差异(GRID1 - GRID2)
预期输出
id name value
001 rajan -100
002 mahesh -100
003 jayan 150
如何查询上述条件
需要查询帮助
答案 0 :(得分:4)
select id, name, sum(case when no = 'grid1' then value else value*(-1) end)
from table1
group by id, name
参见SQL FIDDLE演示