Solved

db2 query with sum in condition

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Closed / connection link failure to DB2 database 3 501
db2 - u 3 233
DB2 CONCAT FAILING 4 111
DB2 iSeries Combine Results of 2 Selects (nested Join?) ? 2 76
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 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