mysql - SQL-Case When Issue -
i trying sort transaction dates aging policy. when lastdate
has been in location greater aging days
limit policy should show overage
if not within
referring current date. here current table:
+---------+------+----------+-------------+ |lastdate | part | location | aging days | +---------+------+----------+-------------+ 12/1/2016 123 vvv 90 8/10/2017 444 rrr 10 8/01/2017 144 pr 21 7/15/2017 12 rrr 10
here query:
select q.lastdate, r.part, r.location, a.agingpolicy 'aging days' opsintranexcel r (nolock) left inventoryaging (nolock) on r.location=a.location left join (select part,max(trandate) lastdate opsintran group part) q on r.part=q.part
here column want added in:
+---------+------+----------+------------+---------+ |lastdate | part | location | aging days | age | +---------+------+----------+------------+---------+ 12/1/2016 123 vvv 90 overage 8/10/2017 444 rrr 10 within 8/01/2017 144 pr 21 within 7/15/2017 12 rrr 10 overage
i appreciate help.
i think below code work you
select q.lastdate, r.part, r.location, a.agingpolicy 'aging days' 'age' = case when datediff( day, q.lastdate, getdate() ) > a.agingpolicy 'overage' else 'within' end opsintranexcel r (nolock) left join inventoryaging (nolock) on r.location=a.location left join ( select part,max(trandate) lastdate opsintran trantype='ii' , perpost>='201601' group part) q on r.part=q.part
Comments
Post a Comment