Saturday, 18 November 2017

Transpose multiple columns to single column name and single row to their corresponding columns

/////////////// Sql problem

SELECT 'Data'as [Totals], sum(isnull([Avare],0))[Avare],sum(isnull([Blackgram],0))[Blackgram],sum(isnull([Cowpea],0))[Cowpea],sum(isnull([Greengram],0))[Greengram],sum(isnull([Horsegram],0))[Horsegram],sum(isnull([Madaki],0))[Madaki],sum(isnull([TotalPulses],0))[TotalPulses],sum(isnull([Tur],0))[Tur] FROM [tbl_AgriCrop_AreaSown]











///////// Solution

SELECT field[Crops],value[Data (Ha.)]FROM(
SELECT  sum(isnull([Avare],0))[Avare],sum(isnull([Blackgram],0))[Blackgram],sum(isnull([Cowpea],0))[Cowpea],sum(isnull([Greengram],0))[Greengram],sum(isnull([Horsegram],0))[Horsegram],sum(isnull([Madaki],0))[Madaki],sum(isnull([TotalPulses],0))[TotalPulses],sum(isnull([Tur],0))[Tur]  FROM [tbl_AgriCrop_AreaSown]
)Dt
unpivot
(
value
for field in ([Avare],[Blackgram],[Cowpea],[Greengram],[Horsegram],[Madaki],[TotalPulses],[Tur])
)un


////// result



No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...