Home » SQL & PL/SQL » SQL & PL/SQL » How to integrate SUM in WHERE?
How to integrate SUM in WHERE? [message #235698] Mon, 07 May 2007 13:01 Go to next message
majstoru
Messages: 68
Registered: October 2006
Location: Serbia
Member
Hi,

Here is my SQL statement, is there any way to fetch only records that SUM(TEZINAULAZ - TEZINAIZLAZ) is greater that 0 (zero)?

SELECT MAGACINI, ARTIKLI, MAGACINIDEO, OZNAKA, SUM(KOLICINAULAZ - KOLICINAIZLAZ) AS KOLICINA, SUM(TEZINAULAZ - TEZINAIZLAZ) AS TEZINA
FROM SM.ZALIHA
WHERE MAGACINI = :MAGACINI AND SUM(KOLICINAULAZ - KOLICINAIZLAZ) > 0
GROUP BY MAGACINI, ARTIKLI, MAGACINIDEO, OZNAKA
ORDER BY MAGACINIDEO

Thanks in advance...
Re: How to integrate SUM in WHERE? [message #235701 is a reply to message #235698] Mon, 07 May 2007 13:13 Go to previous messageGo to next message
Michel Cadot
Messages: 64139
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Have a look at HAVING clause.

Regards
Michel
icon12.gif  Re: How to integrate SUM in WHERE? [message #235706 is a reply to message #235698] Mon, 07 May 2007 13:38 Go to previous messageGo to next message
majstoru
Messages: 68
Registered: October 2006
Location: Serbia
Member
Thanks a lot Michel, I'm new in Oracle and this forum too, are the answers are so fast as this?

Cool
Re: How to integrate SUM in WHERE? [message #235707 is a reply to message #235706] Mon, 07 May 2007 13:50 Go to previous messageGo to next message
Michel Cadot
Messages: 64139
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Most of the time.
There is always at least 1 to poll the forum.

Regards
Michel
Re: How to integrate SUM in WHERE? [message #235708 is a reply to message #235698] Mon, 07 May 2007 13:58 Go to previous messageGo to next message
majstoru
Messages: 68
Registered: October 2006
Location: Serbia
Member
Excelent!!! Cool
Re: How to integrate SUM in WHERE? [message #235712 is a reply to message #235708] Mon, 07 May 2007 14:17 Go to previous messageGo to next message
Michel Cadot
Messages: 64139
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
But don't forget to read:
How to get a quick answer to your question: TIPS AND TRICKS
How to format your posts
Regards
Michel
Re: How to integrate SUM in WHERE? [message #241738 is a reply to message #235712] Wed, 30 May 2007 09:22 Go to previous messageGo to next message
sifison
Messages: 11
Registered: May 2007
Junior Member
You can't use Group Function in a WHERE Clause, You have to use the Having Clause.

For example, you could also use the SUM function to return the name of the department and the total sales (in the associated department). The HAVING clause will filter the results so that only departments with sales greater than $1000 will be returned.

SELECT department, SUM(sales)
FROM order_details
GROUP BY department
HAVING SUM(sales) > 1000;

Re: How to integrate SUM in WHERE? [message #241740 is a reply to message #241738] Wed, 30 May 2007 09:27 Go to previous message
Michel Cadot
Messages: 64139
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Why do you give an answer 3 weeks after the answer was already given?

Regards
Michel
Previous Topic: table design question: storing both character data and numeric data in a VARCHAR2(10)
Next Topic: Working with Arrays
Goto Forum:
  


Current Time: Thu Dec 08 08:26:39 CST 2016

Total time taken to generate the page: 0.25124 seconds