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
 Advanced SQL Query?

Author  Topic 

ttrent
Starting Member

2 Posts

Posted - 2009-04-06 : 11:43:25
Hello,

I'm quite literally brand new to SQL - my job has put me in a position where I need to learn and understand it to work on a project and they've put me on the fire. They aren't really giving me any time to learn it properly so I'm having to go to various tutorial sites and forums to try and piece together what it is we need to do.

I don't really know how to explain so I'll just give an example of data and what I would like to get out of that data in an end result to find out if it's possible. I really appreciate any help you can provide by taking the time to look over this (sorry if the formatting doesn't come out correctly - I tried to get it readable).


Table 1-ORIGINAL

uID firstName destinationOne destinationTwo destinationThree destinationFour
123 Jim Alaska
123 Jim Kansas
123 Jim Texas
521 Maggy Virginia
521 Maggy Idaho
510 Kerry Florida
510 Kerry Georgia
510 Kerry Kansas
981 Dave California
981 Dave Washington
981 Dave Texas
981 Dave Illinois



Table 1-NEW

uID firstName destinationOne destinationTwo destinationThree destinationFour
123 Jim Alaska Kansas Texas
521 Maggy Virginia Idaho
510 Kerry Florida Georgia Kansas
981 Dave California Washington Texas Illinois

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-04-06 : 14:01:48
There are better ways to do this, and 5 miuntes after I post this Peso and/or Visakh will show you how!


DECLARE @Table TABLE
( uid int
,firstName varchar(50)
,destination varchar(50)
)

INSERT INTO @Table


SELECT 123, 'Jim','Alaska' UNION ALL
SELECT 123, 'Jim','Kansas' UNION ALL
SELECT 123, 'Jim','Texas' UNION ALL
SELECT 521, 'Maggy','Virginia' UNION ALL
SELECT 521, 'Maggy','Idaho' UNION ALL
SELECT 510, 'Kerry','Florida' UNION ALL
SELECT 510, 'Kerry','Georgia' UNION ALL
SELECT 510, 'Kerry','Kansas' UNION ALL
SELECT 981, 'Dave','California' UNION ALL
SELECT 981, 'Dave','Washington' UNION ALL
SELECT 981, 'Dave','Texas' UNION ALL
SELECT 981, 'Dave','Illinois'


SELECT t2.uid,t2.firstName
,[destinationOne] = MAX(t2.destinationOne)
,[destinationTwo] = MAX(t2.destinationTwo)
,[destinationThree] = MAX(t2.destinationThree)
,[destinationFour] = MAX(t2.destinationFour)
FROM
(
select t1.uid
,t1.firstname
,[destinationone] = case when t1.row= 1 then t1.destination else null end
,[destinationtwo] = case when t1.row= 2 then t1.destination else null end
,[destinationthree] = case when t1.row= 3 then t1.destination else null end
,[destinationfour] = case when t1.row= 4 then t1.destination else null end
from


(
select uid,firstName,destination,[Row] = ROW_NUMBER() OVER (partition by uid,firstname order by uid)
from @table
) t1
) t2

GROUP BY t2.uid,t2.firstName
ORDER BY uid

Jim
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-07 : 10:35:36
[code]
SELECT uid,firstName
,[1] AS [destinationOne]
,[2] AS [destinationTwo]
,[3] AS [destinationThree]
,[4] AS [destinationFour]
FROM
(
select uid,firstName,destination,[Row] = ROW_NUMBER() OVER (partition by uid,firstname order by uid)
from @table
) m

PIVOT (MAX(destination) FOR Row IN ([1],[2],[3],[4]))p
ORDER BY uid
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-07 : 10:38:00
if max number of destinations cannot be determined then use this
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-04-07 : 12:25:49
Also, look up NORMALIZATION. You data does not appear to be normalized.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

ttrent
Starting Member

2 Posts

Posted - 2009-04-09 : 11:06:03
Thanks for all this, I'm looking at it now
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-10 : 06:19:11
welcome
Go to Top of Page
   

- Advertisement -