2
select planttype ,sum(noof50kgsbags*50)[50 Kg],sum(noof70kgsbags*70)[70 Kg]
from K_FeedPlantEntryIndent
WHERE  (date BETWEEN '2013-04-01 00:00.000' AND getdate()) AND (attrited = 'True')
group by planttype order by planttype

OutPut::

planttype   [50 Kg Bags]
Rohini        56150
Sneha         43950
KJL           353550
Suguna       1290850

Desired Output::

Bags    Rohini   Sneha   KJL   Suguna
 50KgBags    x        xx     xx    xxx

My Query using Pivot::

select * from (select planttype  ,sum(noof50kgsbags*50)[50 Kg] from       K_FeedPlantEntryIndent WHERE  (date BETWEEN '2013-04-01 00:00.000' AND getdate()) AND   (attrited = 'True')
group by planttype)as t
PIVOT
( sum([50 Kg])
for[planttype] in( Rohini,Sneha,KJL,Suguna )
) As t2

Result for this query:

    Rohini   Sneha   KJL   Suguna
     x        xx     xx    xxx

I need Bags also by doing I am getting error . Please Help me out

4

1 回答 1

1

尝试这个:

select '50KgBags' [Bags], * from (select planttype  ,sum(noof50kgsbags*50)[50 Kg] from       K_FeedPlantEntryIndent WHERE  (date BETWEEN '2013-04-01 00:00.000' AND getdate()) AND   (attrited = 'True')
group by planttype)as t
PIVOT
( sum([50 Kg])
for[planttype] in( Rohini,Sneha,KJL,Suguna )
) As t2

SQLFiddle

于 2013-08-06T10:27:35.313 回答