If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Query Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-08-08, 00:25
Dinomike20 Dinomike20 is offline
Registered User
 
Join Date: Feb 2008
Posts: 2
Query Help

Hi,

I have a query where I'm pulling item codes from the items table and then summing the quantity sold from the invoice table. What I need to do is sum the quantity sold from the invoice table, and then in the next column i need to sum it again but only for invoices in a specified date range.

The problem is when I put the date info in the where section, it applies it to both sums. I only want it to apply to the second one.

Is there a way to do this?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 02-08-08, 02:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
SELECT SUM(...)
FROM   ...
UNION ALL
SELECT SUM(...)
FROM   ...
WHERE  ...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 02-08-08, 02:52
Dinomike20 Dinomike20 is offline
Registered User
 
Join Date: Feb 2008
Posts: 2
Quote:
Originally Posted by stolze
Code:
SELECT SUM(...)
FROM   ...
UNION ALL
SELECT SUM(...)
FROM   ...
WHERE  ...

Thanks. I have a GROUP BY section as well, where does this go?
Reply With Quote
  #4 (permalink)  
Old 02-08-08, 03:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select it.itemcode
     , sum(in.quantity) as quantity_sold
     , sum(case when in.solddate >= '2007-12-01'
                 and in.solddate  < '2008-01-01'
                then in.quantity
                else 0 end) as quantity_sold_december
  from items as it
left outer
  join invoices as in
    on in.foo = it.bar
group
    by it.itemcode
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On