| Author |
Topic |
|
diogenes
Starting Member
6 Posts |
Posted - 2011-05-30 : 13:00:10
|
| Hi,I have a table with 4 columnsName Date Value idxLucy 12-20-2010 10 1Lucy 12-20-2010 15 2Lucy 12-20-2010 13 3Lucy 12-20-2010 12 4Luke 12-20-2010 10 1Luke 12-20-2010 15 2Luke 12-20-2010 13 3Luke 12-20-2010 18 4I want to diplay this table asName Date idx1 idx2 idx3 idx4Lucy 12-20-2010 10 15 13 12Luke 12-20-2010 10 15 13 18Any help is appreciated. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-30 : 13:44:44
|
| You can use PIVOT or CASE. This looks like homework, so show us what you've come up with so far and we'll help get the rest of the way.JimEveryday I learn something that somebody else already knew |
 |
|
|
diogenes
Starting Member
6 Posts |
Posted - 2011-05-30 : 14:05:42
|
| It's not a homeworkI just used that exemple to simplified the problem.Actually, my table looks like this:Date Pin Time IDX 2011-03-01 010071125296 07:00:00 12011-03-01 010071125296 08:10:00 22011-03-01 010071125296 09:07:00 32011-03-01 010071125296 10:08:00 4I tried to use pivod and that's what I got:SELECT Date,Pin,[1],[2],[3],[4],[5],[6]FROM ( SELECT idx,pin,date,time from @MyTable) as tbpivot(min(time) for idx in ([1],[2],[3],[4],[5],[6])) as pvDate Pin 1 2 3 42011-03-01 010071125296 07:00:00 NULL NULL NULL2011-03-01 010071125296 NULL 08:10:00 NULL NULL2011-03-01 010071125296 NULL NULL 09:07:00 NULL2011-03-01 010071125296 NULL NULL NULL 10:08:00how can I display it in one line? |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-05-30 : 14:53:50
|
I don't see anything wrong with your code. Here is an example that works correctly. It is using your query exactly as it is (except I changed the table name to #tmp). See if this works and compare with your code.create table #tmp ([date] date, pin varchar(255), [time] time, idx int);insert into #tmp values ('2011-03-01','010071125296','07:00:00','1'),('2011-03-01','010071125296','08:10:00','2'),('2011-03-01','010071125296','09:07:00','3'),('2011-03-01','010071125296','10:08:00','4')SELECT Date,Pin,[1],[2],[3],[4],[5],[6]FROM ( SELECT idx,pin,date,time from #tmp) as tbpivot(min(time) for idx in ([1],[2],[3],[4],[5],[6])) as pvdrop table #tmp;If you are using SQL 2005 or lower, change the data types to datetime instead of date and time. |
 |
|
|
diogenes
Starting Member
6 Posts |
Posted - 2011-05-30 : 15:05:29
|
| the following code works perfectly:select Date,Pin, MIN(CASE WHEN idx = 1 then time ELSE NULL END) AS t1, MIN(CASE WHEN idx = 2 then time ELSE NULL END) AS t2, MIN(CASE WHEN idx = 3 then time ELSE NULL END) AS t3, MIN(CASE WHEN idx = 4 then time ELSE NULL END) AS t4, MIN(CASE WHEN idx = 5 then time ELSE NULL END) AS t5, MIN(CASE WHEN idx = 6 then time ELSE NULL END) AS t6FROM MyTable group by Date,pinthanks! |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-30 : 15:25:14
|
| SO does Sunita's. You may find a performance hit in your way over Sunita's, but hopefully not of any significance.JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|