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 :
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.