Click here to Skip to main content
Licence 
First Posted 23 May 2002
Views 96,862
Bookmarked 34 times

Can you do this in one statement?

By | 24 May 2002 | Article
A SQL conspiracy ...
 
Part of The SQL Zone sponsored by
See Also

Introduction

Ok, here is the problem. Suppose we have a table called SalesRecords in a relational database, like the following:

ID Store Product Sales DateTime
1 DC Apple 1.23

02/03/2002,

07:01:52

2 LA Orange 8.20

02/03/2002,

08:31:21

3 NY Orange 0.77

02/03/2002,

08:24:33

4 DC Watermelon 3.24

02/03/2002,

09:01:24

5 NY Strawberry 6.33

02/03/2002,

10:11:35

6 LA Pear 21.45

02/03/2002,

10:22:55

7 LA Apple 8.88

02/03/2002,

11:50:42

8 DC Orange 13.24

02/03/2002,

13:06:17

... ... ... ... ...

What we want, is a summary report showing total sales of a selected group of products at each store. For example, if we select to see the summary for Apple, Orange and Pear only, then the report should look like the following:

Store TotalAppleSales TotalOrangeSales TotalPearSales
DC 323.50 489.20 1909.02
LA 309.42 1290.23 239.33
NY 2456.80 2301.66 780.12
... ... ... ...

I will show you how to produce the above result in a single SQL statement. Let's first do this in multiple steps so that you can understand the code easily. First we generate the following result using the SQL statement below. Note that the (case when ... then ... else ... end) expression in the SQL statement generates a new column which is simply a 0-1 indicator, any product that is not among the selected set (Apple, Orange, Pear) will get 0 values in the three generated columns.

select
    Store, 
    (case when Product='Apple' then 1 else 0 end) as IsApple, 
    (case when Product='Orange' then 1 else 0 end) as IsOrange, 
    (case when Product='Pear' then 1 else 0 end) as IsPear,
    Sales
from
    SalesRecords
Store IsApple IsOrange IsPear Sales
1 1 0 0 1.23
2 0 1 0 8.20
3 0 1 0 0.77
4 0 0 0 3.24
5 0 0 0 6.33
6 0 0 1 21.45
7 1 0 0 8.88
8 0 1 0 13.24
... ... ... ... ...

Let's assume that the above result was saved in a temporary table ReportTempData. Now we can use the following SQL statement to produce the summary report we wanted.

select
    Store,
    sum(IsApple*Sales) as TotalAppleSales,
    sum(IsOrange*Sales) as TotalOrangeSales,
    sum(IsPear*Sales) as TotalPearSales
from 
    ReportTempData
group by Store

Of course we don't really need the temporary table ReportTempData. Here is the combined single SQL statement that does the job:

select
    Store,
    sum(A.IsApple*A.Sales) as TotalAppleSales,
    sum(A.IsOrange*A.Sales) as TotalOrangeSales,
    sum(A.IsPear*A.Sales) as TotalPearSales
from 
    (
        select
            Store, 
            ( case when Product='Apple' then 1 else 0 end) as IsApple, 
            ( case when Product='Orange' then 1 else 0 end) as IsOrange, 
            ( case when Product='Pear' then 1 else 0 end) as IsPear,
            Sales
        from
            SalesRecords
    )  A   
group by Store

We can further simplify it to a statement with only one select query:

select 
 Store, 
  sum(case when Product='Apple' then Sales else 0 end) as TotalAppleSales, 
  sum(case when Product='Orange' then Sales else 0 end) as TotalOrangeSales, 
  sum(case when Product='Pear' then Sales else 0 end) as TotalPearSales 
from  
  SalesRecords 
group by Store

That's it. Thanks for reading and please check out my other articles and tools.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalchinese people Pinmemberzhaojicheng18:00 5 Mar '07  
GeneralIn SQL2005 Pinmemberr.stropek23:15 12 Jun '06  
QuestionWill it work for other new products? PinmemberJohn PC22:46 11 Jun '06  
Generalcase when else PinmemberMerlinblack16:23 7 Jun '05  
GeneralMy previous query is bad, it is the correct solution PinsussAnonymous2:41 5 Feb '04  
Generalan another one statement solution PinsussAnonymous1:59 5 Feb '04  
GeneralRe: an another one statement solution PinmemberNiklaus22:57 30 Jul '04  
GeneralUpdate 2002-05-25 PinmemberXiangyang Liu22:42 24 May '02  
GeneralMore simplification needed PinmemberAnonymous23:08 24 May '02  
GeneralOver Verbose PinmemberBlake Coverett17:07 24 May '02  
GeneralRe: Over Verbose PinmemberXiangYangLiu22:13 24 May '02  
GeneralAlso see.... PinmemberKlaus Probst13:59 24 May '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 25 May 2002
Article Copyright 2002 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid