Re: Is this query possible?

From: Bret Halford <bret_at_sybase.com>
Date: 1996/07/11
Message-ID: <4s378t$jdu_at_tribune.sybase.com>#1/1


In article <4s1gse$r47_at_xanadu.io.com>, mskc_at_io.com (Casey Claiborne) writes:
|>
|> Hello,
|> I have rows that are similar to the following:
|>
|>
|> YearNo WeekNo DayNo ID Total
|> ------ ------ ----- --- -----
|> 1996 1 1 7 $35.00
|> 1996 1 2 7 $65.00
|> 1996 1 3 7 $25.00
|> 1996 1 4 7 $12.00
|> 1996 1 1 10 $35.00
|> 1996 1 2 10 $65.00
|> 1996 1 3 10 $25.00
|> 1996 1 4 10 $12.00
|> 1996 1 1 12 $35.00
|> 1996 1 2 12 $65.00
|> 1996 1 3 12 $25.00
|> 1996 1 4 12 $12.00
|>
|> I need to get a result of something like ...
|>
|> WeekNo ID Mon Tue Wed Thur Fri.
|> 1 7 $35 $65 $25 $12 0
|> 1 10 $35 $65 $25 $12 0
|> 1 12 $35 $65 $25 $12 0
|>
|>
|> How can I do this? Any help hints or advice would be *greatly*
|> appreciated :) Sorry about the length of this post, I just
|> wanted to be clear -
|>
|> TIA
|>
|>

Try something like:

select WeekNo, ID, 0.0 Mon, 0.0 Tue, 0.0 Wed, 0.0 Thur, 0.0 Fri into summary_table from base_table
go

update summary_table s set s.Mon = b.Total from base_table b
where
s.WeekNo = b.WeekNo and
s.ID = b.ID and

DayNo = 1
go

[code for DayNo 2-5 left as exercise for the reader]

select * from summary_table
go
drop summary_table
go

-- 
---------------------------------------------------------------------
| Bret Halford                     bret_at_sybase.com               ___| 
| Sybase Technical Support         fax (510)-922-3911         __|  
| 6475 Christie Avenue                                       |__   
| Emeryville, CA 94608 USA         exec sp_realitycheck()       |___
|                                                                   |
#####################################################################
Received on Thu Jul 11 1996 - 00:00:00 CEST

Original text of this message