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
8 6080
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
Use Pivot. It is very simple
sorry sir, i am not familiar with Pivot please help me to sort out
Try this Sample data - declare @t table(id int, CertificateName varchar(50))
-
insert into @t
-
select 1 ,'Secondary' union all select 2,'Highersecondary' union all
-
select 3 ,'Undergraduate' union all select 4,'PostGraduate'
Query: - select
-
[Secondary]
-
,[Highersecondary]
-
,[Undergraduate]
-
,[PostGraduate]
-
from (select id,CertificateName from @t)as X
-
pivot
-
(min(id) for CertificateName IN
-
([Secondary],[Highersecondary],[Undergraduate],[PostGraduate]))as Pvt
Output: - Secondary Highersecondary Undergraduate PostGraduate
-
1 2 3 4
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
Try dynamic pivoting Sample data - create table #t(id int, CertificateName varchar(50))
-
insert into #t
-
select 1 ,'Secondary' union all select 2,'Highersecondary' union all
-
select 3 ,'Undergraduate' union all select 4,'PostGraduate'
--Program starts - declare @col_list varchar(max)
-
declare @dynquery varchar(max)
-
select
-
distinct IsNull(@col_list,'') +
-
',[' + cast(CertificateName as varchar(50)) + ']' as list
-
into #temp from #t
-
select @col_list = stuff(namelist,1,1,'') from(select cast(list as varchar(max)) from #temp for xml path (''))X(namelist)
-
-
set @dynquery = 'select * from #t pivot ( min(id) for CertificateName in (' + @col_list + ') ' + ') as pvt'
-
exec (@dynquery)
-
-
drop table #temp
-
drop table #t
Output: - Highersecondary PostGraduate Secondary Undergraduate
-
2 4 1 3
-
Hi,
I have special characters in my column value and I get null values. - create table #t(id int, CertificateName varchar(50))
-
insert into #t
-
select 1 ,'Seco&ndary' union all select 2,'<Highersecondary>' union all
-
select 3 ,'Undergraduate' union all select 4,'PostGraduate'
Output: - <Highersecondary> PostGraduate Seco&ndary Undergraduate
-
NULL 4 NULL 3
-
Please help! @nbiswas Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Mick White |
last post by:
mysql> select * from guestbook;
+----+--------+---------+-----------------+----------------+
| id | fname | lname | comments | time_in |...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |