If PEOPLE
are the same as in other rows but COMPANY
is different, then give me SERVICE_1
; if PEOPLE
are the same like other rows and COMPANY
are also the same then give me SERVICE2
. How can I write code for this in T-SQL? Can it be achieved using the row_number
clause?
Original data:
| PEOPLE | COMPANY | SERVICE_1 | SERVICE_2 |
| ------------ | ------------ | ------------ | ------------ |
| KRISH | AA | HYDRO | WATER |
| KRISH | BB | NULL | WATER |
| JOHN | CC | NULL | ROAD |
| JOHN | CC | NULL | ELECY |
| JOHN | CC | NULL | GAS |
Desired results:
PEOPLE | COMPANY | SERVICE |
---|---|---|
KRISH | AA | HYDRO |
KRISH | BB | NULL |
JOHN | CC | ROAD |
JOHN | CC | ELECY |
JOHN | CC | GAS |
1条答案
按热度按时间pbpqsu0x1#
Probably a dozen ways to do this. One is you can count the distinct companies separately and then join, exposing the service with a CASE expression ( example ):