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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Smart sql to sort the following

Author  Topic 

run
Starting Member

7 Posts

Posted - 2008-02-11 : 11:07:42
Probably simple

I have a table with 4 rows: Name, Event, EventDuration & Trip

A person can have several Events during a single trip, so you can have the following:

name1 | Event1 | EventDuration | Trip1
name1 | Event2 | EventDuration | Trip1
name1 | Event3 | EventDuration | Trip1
name2 | Event1 | EventDuration | Trip2
name2 | Event3 | EventDuration | Trip2
name3 | Event2 | EventDuration | Trip2

I there a smart way to construct an sql statement so I get the following representation:
Name | Trip | Duration Event1 | Duration Event2 | Duration Event3
name1 | Trip1 | EventDuration | EventDuration | EventDuration
name2 | Trip2 | EventDuration | null | EventDuration
name3 | Trip2 | null | EventDuration | null

There are 6 different event types to keep track of

Right now I make 6 different views and join all six, but is there a smart way to just do a single sql statement?

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-02-11 : 11:10:22
Look up PIVOT operator in BOL.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-11 : 11:15:19
You might need to apply PIVOT operator along with dynamic sql if all trips dont contain same number of events.
Go to Top of Page

run
Starting Member

7 Posts

Posted - 2008-02-11 : 11:41:23
Thanks!
Go to Top of Page
   

- Advertisement -