Solved

db2 query with sum in condition

Posted on 2006-07-13
1
2,869 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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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