Click here to Skip to main content
15,888,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
table
---------
ID	 Name	 subject	 marks

1  	Murugan	 tamil		66	
2	Raja	 tamil		82
3	Kumar	 tamil		74
4	Murugan	 English	71
5	Raja	 English	80
6	Kumar	English		69

i need this type output
Name 		tamil 		english

Murugan	 	66		71
Raja	 	82		80
Kumar		74		69
Posted
Updated 5-Jan-12 20:56pm
v2
Comments
Sridhar Patnayak 6-Jan-12 6:33am    
Dynamic creation of columns can be achieved by using pivot-table in sqlserver.
check this link http://blog.sqlauthority.com/2008/05/22/sql-server-pivot-table-example/. Your output can be achieved with pivot-table.

Try this

SQL
CREATE TABLE #tbl_Language
(
ID   BIGINT
,Name    NVARCHAR(50)
,subject     NVARCHAR(50)
,marks NVARCHAR(50)
)


INSERT INTO #tbl_Language
SELECT 1,'Murugan','tamil',66
 UNION ALL
SELECT 2,'Raja','tamil',82
 UNION ALL
SELECT 3,'Kumar','tamil',74
 UNION ALL
SELECT 4,'Murugan','English',71
 UNION ALL
SELECT 5,'Raja','English',80
 UNION ALL
SELECT 6,'Kumar','English',69


 SELECT Name ,tamil,English FROM
   (Select Name, Subject, marks FROM #tbl_Language F) P
   Pivot (MAX(Marks) For subject In (tamil,English)) as Pvt
 
Share this answer
 
Comments
Sridhar Patnayak 6-Jan-12 6:29am    
This is not a good approach
SQL
select distinct(Obj1.Name),Obj2.Mark as 'Tamil',Obj3.Mark as 'English' from c Obj1 join c Obj2 on Obj1.Name=Obj2.Name and Obj2.Subject='Tamil' join c Obj3 on Obj1.Name=Obj3.Name and Obj3.Subject='English' order by Name



C is the Table Name.
Obj1,Obj2,Obj3 are all object of c.

Try this u vil get surely what u want....
 
Share this answer
 

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