SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bunny_hyd_smile
Starting Member

India
1 Posts

Posted - 12/22/2012 :  03:14:41  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 12/22/2012 :  03:25:49  Show Profile  Reply with Quote
is this for a report?

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 12/22/2012 :  03:31:33  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000