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

Only show data > (x) in a specified row SQL

I have the below query and I only want column "Days since last pick" to display values greater than 15. Does anyone know of an easy way to do this?

select distinct srcloc "location",
    max(pckdte) "Date",
    sysdate - max(pckdte) "Days Since Last Pick",
    count(distinct inventory_view.lodnum) "# of Pallets in Location"
from pckwrk_view
left join inventory_view
    on pckwrk_view.srcloc = inventory_view.stoloc
join locmst
    on locmst.stoloc = inventory_view.stoloc
where srcloc like 'LP%A'
    and locmst.useflg = 1
group by srcloc
order by max(pckdte) asc                                                                                                                   

>Solution :

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

I only want column "Days since last pick" to display values greater than 15

If you want to display null for values that are less than 15, use a case expression:

select srcloc "location",
    max(pckdte) "Date",
    case when sysdate - max(pckdte) > 15 then sysdate - max(pckdte) end "Days Since Last Pick",
    count(distinct inventory_view.lodnum) "# of Pallets in Location"
from pckwrk_view
left join inventory_view
    on pckwrk_view.srcloc = inventory_view.stoloc
join locmst
    on locmst.stoloc = inventory_view.stoloc
where srcloc like 'LP%A'
    and locmst.useflg = 1
group by srcloc
order by max(pckdte) asc 

On the other hand, if you want to remove these rows from your resultset, then use a having clause to filter on the aggregate expression:

select srcloc "location",
    max(pckdte) "Date",
    sysdate - max(pckdte) "Days Since Last Pick",
    count(distinct inventory_view.lodnum) "# of Pallets in Location"
from pckwrk_view
left join inventory_view
    on pckwrk_view.srcloc = inventory_view.stoloc
join locmst
    on locmst.stoloc = inventory_view.stoloc
where srcloc like 'LP%A'
    and locmst.useflg = 1
group by srcloc
having sysdate - max(pckdte) > 15
order by max(pckdte) asc 

Note: distinct in a group by query is redundant, and somehow unclear; I removed it.

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