Solved

db2 query with sum in condition

Posted on 2006-07-13
1
2,859 Views
Last Modified: 2008-01-09
I'm running a query and I have had some success so far, BUT that run of success has ended when I added my last condition.
What I am looking to do is getting the total spend by a customer during the year 2005 but only if the total that customer spent was 1500.

SELECT SUM(INVOPR) FROM INHEAD WHERE CUST = 'BLAHBLAH' AND year(IHDATE) = 2005 AND SUM(INVOPR)<=1500

SELECT SUM(INVOPR) FROM INHEAD WHERE CUST = 'BLAHBLAH' AND year(IHDATE) = 2006 AND SUM(INVOPR)<=1500

This is via ODBC, sum works to get the $$$ out, but when I use it as a condition I think that is where it is bombing.
0
Comment
Question by:iceman19330
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
ocgstyles earned 500 total points
ID: 17101662
Hi,

You use the HAVING clause to drill down further when you are aggragating data.  Try this:

SELECT SUM(INVOPR) FROM INHEAD WHERE CUST = 'BLAHBLAH' AND year(IHDATE) = 2005 HAVING SUM(INVOPR)<=1500

Good Luck

- Keith
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question