Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,



I need the data based on Religion Wise details like

col1 col2 col3
Muslim Hindu Jain
but my data is in One column like

Muslim
Hindu
Jain
How can i achieve this.Please help me
Posted

Use PIVOT in SQL Server for this. Have a look at below link for more information on this.

http://msdn.microsoft.com/en-us/library/ms177410.aspx
 
Share this answer
 
Comments
Amir Mahfoozi 21-Dec-11 7:31am    
+5
Hi jagadeesh
Your question is somehow obscure but generally to make a column into multiple columns here is a general approach :

SQL
select * , 'muslim' col1, '' col2 , '' col3 from table1 where religion='muslim'
union
select * , '' col1, 'hindu' col2 , '' col3 from table1 where religion='hindu'
union
select * , '' col1, '' col2 , 'jain' col3 from table1 where religion='jain'



Hope it helps.
 
Share this answer
 
Comments
jagadeesh123qqq 30-Nov-11 3:09am    
Thanks for ur reply..
Its working fine with some extent, now i need the data in district wise ,means i need all the data based on district .
My script is



SELECT dist_name,col1,col2,col3,MAmount,JAmount,PAmount,MNo,Jno,Pno FROM (select 'M' col1, '' col2 , '' col3,SUM(amount) AS MAmount,'' JAmount,'' PAmount,count(app_district) AS MNo, '' JNo , '' PNo,app_district from RegisterFresh where religion='M' Group By app_district
union
select '' col1, 'J' col2 , '' col3,'' MAmount,SUM(amount) AS JAmount,'' PAmount,'' MNo,count(app_district) AS JNo, '' PNo,app_district from RegisterFresh where religion='J' Group By app_district
union
select '' col1, '' col2 , 'P' col3,'' MAmount, '' JAmount ,SUM(amount) AS PAmount,'' MNo, '' JNo ,count(app_district) AS PNo,app_district from RegisterFresh where religion='P' Group By app_district)
AS R
INNER JOIN District D ON D.Dist_code=R.app_district
GROUP By col1,col2,col3,MAmount,JAmount,PAmount,MNo,Jno,Pno,dist_name

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