Results 1 to 4 of 4

Thread: Query Help

  1. #1
    Join Date
    Feb 2008
    Posts
    2

    Unanswered: 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!

  2. #2
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Code:
    SELECT SUM(...)
    FROM   ...
    UNION ALL
    SELECT SUM(...)
    FROM   ...
    WHERE  ...
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  3. #3
    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?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •