Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 13th, 2008, 09:14 PM
seraieis's Avatar
Member
 
Join Date: Apr 2008
Location: Connecticut
Age: 25
Posts: 33
Default SUM function with conditions (>) or (<)

Hello everyone!

I've run into a little snag with SQL (which I am still very new at). What I'm trying to do it run a query again medical claim information and return only claims where the total is over $1,000. There can be multiple lines per claim

i.e.:
CLAIM_ID, PAY_AMT
1, 1000
2, 50
2, 50
3, 900
3, 100

Here is the query:
Expand|Select|Wrap|Line Numbers
  1. SELECT MBR_NUM, CLAIM_ID, SUM(PAY_AMT) AS SUMOF_PAY_AMT
  2. FROM CLAIM
  3. WHERE SUMOF_PAY_AMT > 1000.00
  4. GROUP BY MBR_NUM, CLAIM_ID
  5.  
When I run this, it gives me this error:
Expand|Select|Wrap|Line Numbers
  1. ERROR [42S22][IBM][CLI Driver][DB2] SQL0206N "SUMOF_PAY_AMT" is not valid in the context where it is used. SQLSTATE=42703
Any ideas as to what I'm doing wrong?

Thanks!
Reply
  #2  
Old August 13th, 2008, 09:34 PM
seraieis's Avatar
Member
 
Join Date: Apr 2008
Location: Connecticut
Age: 25
Posts: 33
Default

It's truely gratifying to solve your own problem :)

Expand|Select|Wrap|Line Numbers
  1. SELECT MBR_NUM, CLAIM_ID, SUM(PAY_AMT)
  2. FROM CLAIM
  3. WHERE ACCT_NUM='3001416'
  4. GROUP BY MBR_NUM, CLAIM_ID
  5. HAVING SUM(PAY_AMT) > 1000
  6.  
I didn't understand anything about the HAVING clause.
Reply
  #3  
Old August 14th, 2008, 06:41 AM
Newbie
 
Join Date: Aug 2008
Posts: 24
Default

Hi dude,

We need to avoid WHERE Clause while using GROUP BY Clause , Instead we can use HAVING clause that will be act as WHERE clause for checking aggregate functions

Query :
-----------

SELECT MBR_NUM, CLAIM_ID, SUM(PAY_AMT)
FROM CLAIM
GROUP BY MBR_NUM, CLAIM_ID
HAVING SUM(PAY_AMT) > 1000


Regards

Padmanaban.N
DB2 Database Associate
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles