473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to select the colums value as Column header?

For Example
I have Table name called "tbl_certificate"

Fields as follows

id Certificate name
1 Secondary
2 Highersecondary
3 Undergraduate

i want the output as follows

Secondary Highersecondary Undergraduate

Please anyone help me to solve this
Dec 31 '09 #1
8 6080
code green
1,726 Expert 1GB
Column headings of what?
Dec 31 '09 #2
Hi code Green
Thanks for replying.
For me Column value should be displayed as field name
Table 1
Id Certifiacte Name
1 Secondary
2 HigherSecondary
3 UnderGraduate
4 PostGraduate

The Output Should be as follows
Secondary HigherSecondary UnderGraduate PostGraduate
Jan 2 '10 #3
nbiswas
149 100+
Use Pivot. It is very simple
Jan 4 '10 #4
sorry sir, i am not familiar with Pivot please help me to sort out
Jan 4 '10 #5
nbiswas
149 100+
Try this

Sample data

Expand|Select|Wrap|Line Numbers
  1. declare @t table(id int, CertificateName varchar(50))
  2. insert into @t 
  3.     select 1 ,'Secondary' union all select 2,'Highersecondary' union all
  4.     select 3 ,'Undergraduate' union all select 4,'PostGraduate' 
Query:

Expand|Select|Wrap|Line Numbers
  1. select         
  2.         [Secondary]
  3.         ,[Highersecondary]
  4.         ,[Undergraduate]
  5.         ,[PostGraduate]
  6.     from (select id,CertificateName from @t)as X
  7.     pivot
  8.     (min(id) for CertificateName IN
  9.     ([Secondary],[Highersecondary],[Undergraduate],[PostGraduate]))as Pvt
Output:

Expand|Select|Wrap|Line Numbers
  1. Secondary    Highersecondary    Undergraduate    PostGraduate
  2. 1    2    3    4
Jan 4 '10 #6
it works well but the no of rows are dynamic
in that case how can i do it
i mean these are dynamic
"Secondary Highersecondary Undergraduate PostGraduate "
any value can be there instead of above and no of value can be dynamic
Jan 4 '10 #7
nbiswas
149 100+
Try dynamic pivoting

Sample data

Expand|Select|Wrap|Line Numbers
  1. create  table #t(id int, CertificateName varchar(50))
  2. insert into #t 
  3.     select 1 ,'Secondary' union all select 2,'Highersecondary' union all
  4.     select 3 ,'Undergraduate' union all select 4,'PostGraduate' 
--Program starts
Expand|Select|Wrap|Line Numbers
  1. declare @col_list varchar(max)
  2. declare @dynquery varchar(max)
  3. select 
  4. distinct   IsNull(@col_list,'') + 
  5. ',[' + cast(CertificateName as varchar(50)) + ']' as list 
  6. into #temp from #t
  7. select @col_list = stuff(namelist,1,1,'') from(select cast(list as varchar(max)) from #temp for xml path (''))X(namelist)
  8.  
  9. set @dynquery = 'select * from #t pivot ( min(id) for CertificateName in (' + @col_list + ') ' + ') as pvt'
  10. exec (@dynquery)
  11.  
  12. drop table #temp
  13. drop table #t
Output:
Expand|Select|Wrap|Line Numbers
  1. Highersecondary    PostGraduate    Secondary    Undergraduate
  2. 2    4    1    3
  3.  
Jan 4 '10 #8
shilpz
1
Hi,

I have special characters in my column value and I get null values.

Expand|Select|Wrap|Line Numbers
  1. create  table #t(id int, CertificateName varchar(50))
  2. insert into #t 
  3.     select 1 ,'Seco&ndary' union all select 2,'<Highersecondary>' union all
  4.     select 3 ,'Undergraduate' union all select 4,'PostGraduate' 
Output:
Expand|Select|Wrap|Line Numbers
  1. &lt;Highersecondary&gt;    PostGraduate    Seco&amp;ndary    Undergraduate
  2. NULL    4    NULL    3
  3.  
Please help!

@nbiswas
Dec 12 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mark | last post by:
good spam subject ;). anyway, i'm alittle stumped. i'm in need of putting together a query that gets the next highest salary ( select max ( sal ) - 1?, from an emp_sal type table. another...
5
by: Joy | last post by:
Hi guys, I am in a tricky situation, I really really hope someone will help me. I got a layout with a main container and three colums inside. Main container: 100% height; 90% width; float: left...
4
by: Mick White | last post by:
mysql> select * from guestbook; +----+--------+---------+-----------------+----------------+ | id | fname | lname | comments | time_in |...
0
by: Bernard Dhooghe | last post by:
In http://www-1.ibm.com/partnerworld/pwhome.nsf/weblook/eac_a_webcast_052504.html the writer says (page 50, Multidimensional Clustering Advisor): " The MDC Advisor feature of the DB2 Design...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
0
by: Michael Seele | last post by:
hi! i need a list of all primary keys in my database! for this reson i build these sql-statement: /SELECT pg_class.relname AS tableName, pg_constraint.conname AS pkName, pg_constraint.conkey AS...
33
by: Peter | last post by:
People are telling me it is bad to put select * from <atable> in a view. I better should list all fields of the table inside the definition of the view. I dont know exactly why but some...
4
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.