March 26, 2013 at 2:26 pm
I need a query to get the required output..
DECLARE @Table TABLE(
stid INT,
stuName VARCHAR(200),
studwor varchar(100)
)
INSERT INTO @Table SELECT 255,'ChrisName','Chris'
INSERT INTO @Table SELECT 179,'ChrisCity','SAN City'
INSERT INTO @Table SELECT 179,'ChrisCounty','Wendy'
INSERT INTO @Table SELECT 179,'ChrisphoneNumber','This is a test phone'
INSERT INTO @Table SELECT 180,'ChrisDName','WTS Test'
INSERT INTO @Table SELECT 180,'ChrisDType','This is for Dtpetest'
Sourcetable:
stid stuNamestudwor
----- ----------------
255ChrisNameChris
179ChrisCitySAN City
179ChrisCountyWendy
179ChrisphoneNumber This is a test phone
180ChrisDNameWTS Test
180ChrisDTypeThis is for Dtpetest
Expected output:
-----------------
stid ChrisName ChrisCity ChrisCounty ChrisphoneNumberChrisDName ChrisDType
----- ---------- ----------- ------------ ------------------------- ----------
255 Chris
179SAN CITY Wendy This is a test phone
180 WTS TestThis is for Dtpetest
Thanks for you help in advance
March 26, 2013 at 2:35 pm
Duplicate post. direct all replies here. http://www.sqlservercentral.com/Forums/Topic1435217-392-1.aspx
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 26, 2013 at 8:41 pm
Thanks the expected output is different....
March 27, 2013 at 4:17 am
Again, I think more info would help but this will get the result you asked for.
select
stid
,max(ChrisName) ChrisName
,max(ChrisCity) ChrisCity
,max(ChrisCounty) ChrisCounty
,max(ChrisphoneNumber) ChrisphoneNumber
from
(select * from @Table) as src
pivot
(
max(studwor)
for stuName in (
ChrisName
,ChrisCity
,ChrisCounty
,ChrisphoneNumber
)
) as pvt
group by stid
April 3, 2013 at 3:53 am
Have posted a reply here :
http://www.sqlservercentral.com/Forums/Topic1435217-392-1.aspx
You might find it useful.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply