Saturday, 4 May 2013

Combine two or more column in one column in sql sever


Combines Two Or More Columns in one Column
Dear Friends,
Some time we need to select two or more columns in single columns ,
if You need this type of output in select statement then check the following article.
Example
step1 : Create a table
create table barcharts(year int ,steps varchar(50),a int ,b int,C INT)

step2 : Insert data into table
insert into barcharts(year,steps,a,b,C) values(2009,'step1',1,5,27) insert into barcharts(year,steps,a,b,C) values(2009,'step2',2,4,25) insert into barcharts(year,steps,a,b,C) values(2009,'step3',3,3,24) insert into barcharts(year,steps,a,b,C) values(2009,'step4',4,2,23) insert into barcharts(year,steps,a,b,C) values(2009,'step5',5,1,21) insert into barcharts(year,steps,a,b,C) values(2010,'step1',10,50,12) insert into barcharts(year,steps,a,b,C) values(2010,'step2',20,40,4) insert into barcharts(year,steps,a,b,C) values(2010,'step3',30,30,6) insert into barcharts(year,steps,a,b,C) values(2010,'step4',40,20,7) insert into barcharts(year,steps,a,b,C) values(2010,'step5',50,10,9) insert into barcharts(year,steps,a,b,C) values(2011,'step1',15,55,12) insert into barcharts(year,steps,a,b,C) values(2011,'step2',25,45,23) insert into barcharts(year,steps,a,b,C) values(2011,'step3',35,35,34) insert into barcharts(year,steps,a,b,C) values(2011,'step4',45,25,45) insert into barcharts(year,steps,a,b,C) values(2011,'step5',55,15,56)

step3 : Write a Following query and Check a output
select year,steps,A ,head from( SELECT year,steps,b A,'b' as head FROM barcharts union ALL SELECT year,steps,a A,'a' as head FROM barcharts UNION ALL SELECT year,steps,C A,'C' as head FROM barcharts )t order by YEAR,HEAD

1 comment:

8451831077