Gopal
Gopal

Reputation: 11992

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

Upvotes: 1

Views: 98

Answers (1)

Grisha Weintraub
Grisha Weintraub

Reputation: 7996

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

see SQL FIDDLE Demo

Upvotes: 4

Related Questions