🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.databases.ms-sqlserver
Article #53

Re: Need help transforming results set format

#53
From: Michael Bilhartz
Date: Thu, 24 Apr 1997 00:00
52 lines
1749 bytes
Brent Eubanks wrote:
> 
> I have a query that generates a results set of the following format:
> 
> DAY CODE TOTAL
> --- ---- -----
> mon   A    5
> tue   A   10
> wed   A    7
> fri   A   20
> sat   A    5
> mon   B   12
> thu   B    3
> fri   B   16
> sat   B    9
> sun   B   23
> tue   C    5
> wed   C   21
> sat   C   17
> 
> I need to put it into the following format:
> 
> Day   A   B   C
> ---  --- --- ---
> mon   5  12   0
> tue  10   0   5
> wed   7   0  21
> thu   0   3   0
> fri  20  16   0
> sat   5   9  17
> sun   0  23   0
> 
> Can anyone suggest a way to do this other than inserting each value
> with an individual update statement?  (There are 21 values in this
> sample set.  The real thing is much to big to do peicemeal.)

> . . .

I think this will work but I haven't tested it:

SELECT Day, 	MAX(CASE WHEN Code = A THEN Total ELSE 0 END) A, 
		MAX(CASE WHEN Code = B THEN Total ELSE 0 END) B, 	
		MAX(CASE WHEN Code = C THEN Total ELSE 0 END) C 
FROM Days
GROUP BY Day

Prior to the CASE statement, this was a lot harder to do, you had to figure 
out a way to do it with the ISNULL function.

Regards and good luck,
-Mike Bilhartz
Technical Details
Message-ID: <335FA2C3.15D6@patriot.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <5jn3g3$2nd@gap.cco.caltech.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 104de1,5ce765e83b646b2f
X-Google-Attributes: gid104de1,public
X-Google-Thread: 11355a,5ce765e83b646b2f
X-Google-Attributes: gid11355a,public
From: Michael Bilhartz <bilhartz@patriot.net>
Subject: Re: Need help transforming results set format
Date: 1997/04/24
Message-ID: <335FA2C3.15D6@patriot.net>
X-Deja-AN: 237166398
References: <5jn3g3$2nd@gap.cco.caltech.edu>
Organization: PatriotNet, (703) 277-7737
Newsgroups: comp.databases.ms-sqlserver,bit.listserv.sqlinfo