| Author |
Topic |
|
emyk
Yak Posting Veteran
57 Posts |
Posted - 2012-10-09 : 12:03:36
|
I have been working on this for weeks now. So far I have not found a solution that works for me. So changed my original scope on this task.So here is what I have now:I have a list of names with a timestamp per names per day (as showed below), where I need to rotate the time column so that it shows the time for each names per day horizontal.I am on SQL 2008 server and trying to see if PIVOT (Pivot by the time column) will accomplish the task here.Any assistant is greatly appreciated.(time)NAME ID TYPE DATE TIME Bob, Henry 1467157 Q 2012-10-07 14:00 Bob, Henry 1467157 Q 2012-10-07 22:19 Bob, Henry 1467157 Q 2012-10-08 14:09 Bob, Henry 1467157 Q 2012-10-08 22:05 Alex,Prince 1467157 Q 2012-10-07 14:00 Alex,Prince 1467157 Q 2012-10-07 22:19 Alex,Prince 1467157 Q 2012-10-07 23:19 Alex,Prince 1467157 Q 2012-10-07 23:29 Alex,Prince 1467157 Q 2012-10-08 14:09 Alex,Prince 1467157 Q 2012-10-08 22:05 NAME ID TYPE DATE T1 T2 T3 T4 Bob, Henry 1467157 Q 2012-10-07 14:00 22:19 Bob, Henry 1467157 Q 2012-10-08 14:09 22:05 Alex,Prince 1467157 Q 2012-10-07 14:00 22:19 23:19 23:29 Alex,Prince 1467157 Q 2012-10-08 14:09 22:05 Alex,Prince 1467157 Q 2012-10-08 22:05 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-10-09 : 12:38:12
|
| [code]SELECT NAME, ID, TYPE, [1] AS T1, [2] AS T2, [3] AS T3, [4] AS T4FROM (SELECT NAME, ID, TYPE, DATE, TIME, ROW_NUMBER() OVER (PARTITION BY Name,ID,Type,Date ORDER BY Time) AS rownumFROM myTable) aPIVOT(MAX(Time) FOR rownum IN([1],[2],[3],[4])) b[/code] |
 |
|
|
emyk
Yak Posting Veteran
57 Posts |
Posted - 2012-10-09 : 13:52:27
|
| thanks robvolk,How do I make the columns dynamic? Can I do a COUNT on the MAX TIME and make my columns length be the MAX count? does this makes sense?[1] AS T1, [2] AS T2, [3] AS T3, [4] AS T4 |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
|
|
emyk
Yak Posting Veteran
57 Posts |
Posted - 2012-10-10 : 10:50:58
|
I keep getting Incorrect syntax near ','. Here is what I haveWITHCTEAAS(SELECT ..FROM TABLE),CTEBAS(SELECT NAME,EmpID, rtyp,[1] AS T1, [2] AS T2, [3] AS T3, [4] AS T4 FROM (NAME, EmpID, rtyp,Date,Time,ROW_NUMBER() OVER (PARTITION BY NAME,EmpID, rtyp,Date ORDER BY Time) AS rownumFROM CTEA) aPIVOT(MAX(Time) FOR rownum IN([1],[2],[3],[4])) b |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-10-10 : 11:03:51
|
| What's the full SQL statement? Need everything for CTEA including the WITH directive. |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-10-10 : 11:09:02
|
Fix up this part:CTEBAS(SELECT NAME,EmpID, rtyp,[1] AS T1, [2] AS T2, [3] AS T3, [4] AS T4 FROM (SELECT NAME, EmpID, rtyp,Date,Time,ROW_NUMBER() OVER (PARTITION BY NAME,EmpID, rtyp,Date ORDER BY Time) AS rownumFROM CTEA) aPIVOT(MAX(Time) FOR rownum IN([1],[2],[3],[4])) b)SELECT * FROM CTEB |
 |
|
|
emyk
Yak Posting Veteran
57 Posts |
Posted - 2012-10-10 : 11:47:42
|
| Thank you! That took care of it. Now I see my data's as expected. |
 |
|
|
|
|
|