February 2, 2012 at 1:26 pm
Create Table PivotTable (ID int, Name varchar(12), type varchar(18), PhoneNumber varchar(6))
insert into PivotTable (ID,Name,type,PhoneNumber)
select 1,'tom','Home',1
union all
select 1,'tom','Office',2
union all
select 2,'trinity','Home',3
union all
select 2,'trinity','Office',4
I am tryin to get phone number in cross tab pivot result like this
id Name home Office
1 tom 1 2
2 trinity 3 4
any help?
thanks in advance.
February 2, 2012 at 1:35 pm
what have you tried so far?
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
February 2, 2012 at 2:55 pm
J Livingston SQL (2/2/2012)
what have you tried so far?
This is just a sample snippet of the code. I have a much larger table to take care of. I was some logical inputs because i need to get this done asap.
February 2, 2012 at 3:02 pm
How about this:
SELECT ID,
NAME,
MAX(CASE WHEN type='Home' THEN PhoneNumber ELSE NULL END) AS [home],
MAX(CASE WHEN type='Office' THEN PhoneNumber ELSE NULL END) AS [Office]
FROM PivotTable
GROUP BY ID,Name
Hope this helps,
David
DBA Dash - Free, open source monitoring for SQL Server
February 2, 2012 at 4:11 pm
wiseman82 (2/2/2012)
I've never cared much for the new syntax, but you can also use:
SELECT ID,
NAME,
pvt.Home,
pvt.Office
FROM PivotTable
PIVOT(
MAX(PhoneNumber)
FOR [TYPE] IN([Home],[Office])
) AS pvt
thanks.really helpful.
February 5, 2012 at 2:41 pm
wiseman82 (2/2/2012)
I've never cared much for the new syntax, but you can also use:
Ditto that.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply