Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Oracle SQL WHEN condition in SELECT statement

I have the following select statement (example) which gives back a sum from a table.

Select SUM(Salary) from Users d, MoreData m where
d.Office = 'A' and
m.Active = 'Yes' and
d.id_user = m.id_of_user

This works perfectly fine and adds up column salery for all users which are located in Office A abd are marked as active in the table MoreData.

Now I would only like to get a result if the SUM(Salary) under these conditions is greater than 1.000.000 EUR. If not the result would be just NULL.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in Advance

I tried adding an CASE WHEN statement but this always gives ORA-00900 back.

It needs to be an SELECT statement.

>Solution :

That looks like a having clause:

Select SUM(Salary) from Users d, MoreData m where
d.Office = 'A' and
m.Active = 'Yes' and
d.id_user = m.id_of_user
having sum(salary) > 1000000       --> this

Though, I presume that you’d actually want to get some more info, such as which user has such a salary, so you’d have to add that column into the select column list and then group by it. Also, to follow good practice, switch to join:

  SELECT d.id_user, 
         SUM (salary)
    FROM users d JOIN moredata m ON m.id_of_user = d.id_user
   WHERE     d.office = 'A'
         AND m.active = 'Yes'
GROUP BY d.id_user
  HAVING SUM (salary) > 1000000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading