Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query

Author  Topic 

bunny_hyd_smile
Starting Member

1 Post

Posted - 2012-12-22 : 03:14:41
Hi,

Need solution for the below query.

Empno Ename
10 Raju
20 Ramu

I need it in the below order.


Empno 10 20
Ename Raju Ramu

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-22 : 03:25:49
is this for a report?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-22 : 03:31:33
see illustration below


declare @t table
(
Empno int,
Ename varchar(10)
)
insert @t
values(10, 'Raju'),
(20, 'Ramu')


select cat,
MAX(CASE WHEN seq=1 THEN value END) AS Val1,
MAX(CASE WHEN seq=2 then value END) AS Val2
from
(
select *,ROW_NUMBER() OVER (PARTITION BY cat ORDER BY value) AS Seq
from (SELECT CAST(Empno AS varchar(10)) AS Empno,Ename FROM @t)t
unpivot (value for cat in (Empno,Ename))u
)m
group by cat



output
-------------------------------------
cat Val1 Val2
-------------------------------------
Empno 10 20
Ename Raju Ramu



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -