Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ABC college in that 3 (X,Y,Z)programs are there programe wise spend some money like this


this is my table

clgname    programe   particularid     f1   

ABC           X             1          5000

ABC	      y             2           2000

ABC           Z             3           4000




How to use the T-SQL select command on this table to get the following output below:

                              X programe       y programe        z programe

particularid

      1                      5000           Null or 0             Null or 0


      2                        Null or 0          2000               Null or 0

      3                      Null or 0            Null or 0            4000



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 8-Apr-13 0:50am
v3

SQL
with tbl as
(
select 'ABC'  clgname   ,      'X'  programe    ,       1   particularid,       5000 f1
union all
select 'ABC'    ,      'y'  ,           2       ,    2000
union all
select 'ABC',           'Z',             3    ,       4000
)
select particularid,
Isnull([x],0) as [X programe],[y] as [Y programe],[z] as [Z programe]
from tbl
pivot (sum(f1) for programe in ([x],[y],[z]))as pvt_tbl

Happy Coding!
:)
 
Share this answer
 
Comments
srikrishnach 10-Apr-13 3:03am    
in this same table am having 500 colleges,some colleges having all programs some colleges having 1 programe, some having 2 programs how to write only one select command at a time
Aarti Meswania 10-Apr-13 3:05am    
you should check links for Dynamic pivoting
http://www.codeproject.com/Articles/27776/Dynamic-Pivoting-in-SQL-Server
http://stackoverflow.com/questions/11985796/sql-server-pivot-dynamic-columns-no-aggregation
http://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query
You can use this by using pivot Query in sql server 2008
 
Share this answer
 
in this same table am having 500 colleges,some colleges having all programs some colleges having 1 programe, some having 2 programs how to write only one select command at a time
 
Share this answer
 
i have a ABC college in that 3 (X,Y,Z)programs are there programe wise spend some money like this


this is my table

clgname programe particularid f1 f2

ABC X 1 5000 1000

ABC y 2 2000 0

ABC Z 3 4000 3000

MNO X 2 2000 0

PQR z 5 2000 5000



how to use select commend in this table i want like bellow table



this table is ABC clgname wise


particularid X programe y programe z programe

f1 f2 f1 f2 f1 f2

1 5000 1000 Null or 0 0 Null or 0 0


2 Null or 0 0 2000 0 Null or 0 0

3 Null or 0 0 Null or 0 0 4000 3000



same as i have 2000 clg are there in that some having only x programe ,some having y programe, some having 2 programs like this how to write for this select command



this code is working but am donot write select command for every college wise......


select particularid,
Isnull([x],0) as [X programe],[y] as [Y programe],[z] as [Z programe]
from tbl
pivot (sum(f1) for programe in ([x],[y],[z]))as pvt_tbl
 
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