Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Filtering an alias (applying a where)

543efe04-a6da-46cd-a519-ce8d60bed028Aug 27 2018 — edited Aug 28 2018

Hello,

I have this query that returns the chemicals present in a material, among other things:

SELECT

    material.material_id,

    material.number,

    material.name,

    onunumber.code,

    COUNT(material.material_id) AS Chems

FROM

    material

LEFT JOIN onunumber ON onunumber.id = material.onunumber_id

LEFT JOIN material_x_chemical ON material_x_chemical.material_id = material.material_id

WHERE

    material.name LIKE '% @CommercialNameQuery %' AND

    (@OnlyWithONU = 0 OR onunumber.code IS NOT NULL ) AND

    (@MinChemicals = ' ' OR Chems >= @MinChemicals) (doesn't work)

GROUP BY

    material.material_id,

    material.number,

    material.name,

    onunumber.code

ORDER BY

    @OrderBy

Now, I gotta improve it where I can filter only results with more than a certain number of chemicals. The problem is, I'm getting the chemicals with an Alias as you can see, and I can't run a WHERE Chems > chemicals , or whatever.

I have seen some examples of using subqueries but I'm not quite following. Any help on how to do this?

Note: things with @ are variables coming from a form/table

This post has been answered by mathguy on Aug 27 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 25 2018
Added on Aug 27 2018
7 comments
1,972 views