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)
 query(rows to columns)

Author  Topic 

arifktdm
Starting Member

6 Posts

Posted - 2002-09-04 : 11:13:43
I have a table

npa time value
202 1 100
202 2 50
202 3 40
.
.
202 24 1


I need a query to display data in the format:
npa 1 2 3 4 ..... 24
202 100 50 40 1


each npa will have 24 rows(time 1 to 24 hrs)
Thanx in advance



robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-09-04 : 11:15:50
Take a look at these articles:

http://www.sqlteam.com/SearchResults.asp?SearchTerms=cross+tab

Also search SQL Team for "CASE", the CASE expression is the best way to do this. Since you have a fixed number of columns, you probably don't need to use the dynamic cross tab stuff.

Go to Top of Page

lozitskiy
Starting Member

28 Posts

Posted - 2002-09-05 : 10:27:07
Dynamic query, poor performance but it works....

create table ptime(npa int, ptime int, total int)

insert ptime values(205,1,24)
insert ptime values(205,1,24)
insert ptime values(205,4,24)
insert ptime values(205,1,12)
insert ptime values(205,1,24)
insert ptime values(205,6,64)
insert ptime values(205,8,4)
insert ptime values(205,24,2)

declare @sql varchar(8000),@ttime int

set @ttime=1
set @sql='select npa '+char(13)
while @ttime<25 begin

select @sql=@sql+',(select sum(total) from ptime where npa=p.npa and ptime='+
cast(@ttime as varchar)+')'+char(13)
select @ttime=@ttime+1
end

select @sql=@sql+'from ptime p
group by npa'
exec(@sql)


-------------
MCP MSSQL
Go to Top of Page
   

- Advertisement -