sql query to convert column to row

given the table below
ID    Event        Time  
1      M               05:00:00.0000000
1      T               06:00:00.0000000
1      A               07:00:00.0000000
2      M               01:00:00.0000000
2      A               02:00:00.0000000
3      T               04:00:00.0000000

what is the sql query that gives me
ID    A             M               T
1     7              5                6
2     2              1
3                                       4

 how about if Event column takes more than 3 values (i.e. 30)? can the query be generic and does not rely on the Event values?      
hamid441Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TempDBACommented:
you can use pivot for this.
0
TempDBACommented:
0
skullnobrainsCommented:
assuming you can enumerate the possible contents of event columns, many ways available looking like this one

select Id, sum(if(event='A',time,0)) as A, sum(if(event='B',time,0)) as B,  ... from table group by Id
0
lluddenCommented:
You can use a dynamic pivot table if you have an unknown number of events

DECLARE @PivotColumnHeaders varchar(MAX)
SELECT @PivotColumnHeaders =
  COALESCE(
    @PivotColumnHeaders + ',[' + UC.Event + ']',
    '[' + Event + ']'
  )
FROM (SELECT Event FROM #T1 GROUP BY Event) UC

DECLARE @PQuery varchar(MAX) = '
SELECT * FROM (SELECT ID, Event, datepart(hh,[Time]) AS [Time] FROM #T1  T0) T1
PIVOT (SUM([Time]) FOR Event IN (' + @PivotColumnHeaders + ') ) AS P'
EXECUTE (@PQuery)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.