Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivot with all data only column change

I need to convert SQL table into expected o/p.

Current table

id   status month
100  P      August
101  D      August
101  P      August
102  P      August
102  P      sept

expected o/p:

id   August Sept
100  P      NULL 
101  D      NULL
101  P      NULL
102  P      P

I need all records.

like image 867
soniya Avatar asked Nov 19 '25 10:11

soniya


1 Answers

Use Pivot.

select *  FROM (
SELECT id  , status, month,
ROW_NUMBER() over( partition by id,month order by id,status )rnk
FROM #tableName w 

) up
PIVOT (  max(status) FOR month IN ([August],[Sept])) AS pvt
like image 114
Mr. Bhosale Avatar answered Nov 21 '25 01:11

Mr. Bhosale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!