Solved

db2 query with sum in condition

Posted on 2006-07-13
1
2,809 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
1 Comment
 
LVL 5

Accepted Solution

by:
ocgstyles earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now