简体   繁体   中英

How to find the difference between 2 rows

Table1

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

I want to find the difference (GRID1 - GRID2) for each id

Expected output

id  name value

001 rajan -100
002 mahesh -100
003 jayan 150

How to make a query for the above condition

Need query help

select id, name, sum(case when no = 'grid1' then value else value*(-1) end)
from table1
group by id, name

see SQL FIDDLE Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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