Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
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

result
planttype   50 Kg	70 Kg
KJL	   353550	2416120
Rohini	   56150	220990
Sneha	   43950        0
Suguna	   1290850	0


My desired output is
plantype   KJL  Rohini Sneha  Suguna
50 kg       xx   xx     xx      xx
70 Kg        x   x      x       xx


Can any one suggest me how to do?
Posted
Updated 5-Aug-13 20:35pm
v4
Comments
Maciej Los 6-Aug-13 2:42am    
What have you tried? Where are you stuck?
Bhagavan Raju M 6-Aug-13 3:03am    
select * from
(select planttype ,sum(noof50kgsbags)[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
Bhagavan Raju M 6-Aug-13 3:04am    
@Maciej Los I am getting 1 as my row no I need 50kg

1 solution

You need to use a pivot to achieve this. Here's an article with a full example for you: Simple Way To Use Pivot In SQL Query[^]
 
Share this answer
 
Comments
Maciej Los 6-Aug-13 8:08am    
Very good answer, +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900