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 2000 Forums
 Transact-SQL (2000)
 Rotating a table

Author  Topic 

Edoode
Starting Member

6 Posts

Posted - 2004-10-12 : 07:47:00
Hi There,

A simple problem, but not perhaps a simple answer..

I have a table(hour int, min int) filled with

hr mn
-- --
06 41
07 11
07 41
07 58
08 13
08 28
08 40
08 43
(and so on until hour 26)
26 11

And I need output like this (NA means NULL)
06 07 08 .. 26
-- -- -- -- --
41 11 13 .. 11
NA 41 28 .. NA
NA 58 40 .. NA
NA NA 43 .. NA

Anyone?

The database I'm using is SQL server 2000.

Regards,

-Edo


-------------------------
Swords don't run out of ammo

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2004-10-12 : 08:09:42
Lookup CROSSTAB in these forums.

Mark
Go to Top of Page

Edoode
Starting Member

6 Posts

Posted - 2004-10-12 : 08:17:39
Thank you. I had already read a number of topics about CROSSTAB, but they all use some agregation, which I do not want.


-------------------------
Swords don't run out of ammo
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-12 : 10:14:21
here ya go


Declare @times table (hr int, mn int)
Insert Into @times
Select 06, 41
Union All Select 07, 11
Union All Select 07, 41
Union All Select 07, 58
Union All Select 08, 13
Union All Select 08, 28
Union All Select 08, 40
Union All Select 08, 43
Union All Select 26, 11

--Select * From @times

Declare @times2 table (id int, hr int, mn int)
Insert Into @times2
Select id = (Select count(*) From @times where hr = A.hr and mn <= A.mn), hr, mn From @times A

Select
rowId = A.id,
hr01 = (Select id from @times2 Where hr = 1 and id = A.id),
hr02 = (Select id from @times2 Where hr = 2 and id = A.id),
hr03 = (Select id from @times2 Where hr = 3 and id = A.id),
hr04 = (Select id from @times2 Where hr = 4 and id = A.id),
hr05 = (Select id from @times2 Where hr = 5 and id = A.id),
hr06 = (Select id from @times2 Where hr = 6 and id = A.id),
hr07 = (Select id from @times2 Where hr = 7 and id = A.id),
hr08 = (Select id from @times2 Where hr = 8 and id = A.id),
hr09 = (Select id from @times2 Where hr = 9 and id = A.id),
hr10 = (Select id from @times2 Where hr = 10 and id = A.id),
hr11 = (Select id from @times2 Where hr = 11 and id = A.id),
hr12 = (Select id from @times2 Where hr = 12 and id = A.id),
hr13 = (Select id from @times2 Where hr = 13 and id = A.id),
hr14 = (Select id from @times2 Where hr = 14 and id = A.id),
hr15 = (Select id from @times2 Where hr = 15 and id = A.id),
hr16 = (Select id from @times2 Where hr = 16 and id = A.id),
hr17 = (Select id from @times2 Where hr = 17 and id = A.id),
hr18 = (Select id from @times2 Where hr = 18 and id = A.id),
hr19 = (Select id from @times2 Where hr = 19 and id = A.id),
hr20 = (Select id from @times2 Where hr = 20 and id = A.id),
hr21 = (Select id from @times2 Where hr = 21 and id = A.id),
hr22 = (Select id from @times2 Where hr = 22 and id = A.id),
hr23 = (Select id from @times2 Where hr = 23 and id = A.id),
hr24 = (Select id from @times2 Where hr = 24 and id = A.id),
hr25 = (Select id from @times2 Where hr = 25 and id = A.id),
hr26 = (Select id from @times2 Where hr = 26 and id = A.id)
From
(Select Distinct id From @times2) A


Corey
Go to Top of Page

Edoode
Starting Member

6 Posts

Posted - 2004-10-12 : 14:18:08
This is exactly what I need (after replacing the Select id from @times2 with Select mn from @times2)

Many thanks!

-Edo

-------------------------
Swords don't run out of ammo
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-12 : 14:22:39
quote:
Originally posted by Edoode

This is exactly what I need (after replacing the Select id from @times2 with Select mn from @times2)

Many thanks!

-Edo

-------------------------
Swords don't run out of ammo



Ooops... yeah thats what I meant ... well at least you got it

Corey
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-10-12 : 20:53:58
Min and Max are aggregate functions that work with all datatypes, and if you only have one value for that combination of row and column, you can use either function and get the same results. You're not forced to use Sum, Avg, and Count.
Go to Top of Page
   

- Advertisement -