danielsmeyer
8/22/2019 - 3:29 PM

On Hand

select stock.catalogno,cat.releaseartist, cat.releasetitle, cat.releasedate, stock_qty, onhand.order_total, (stock_qty-onhand.order_total) as on_hand, coalesce((stock_qty-onhand.order_total), stock_qty) as karsten from kdgstock as stock left join (select p.catalogno, sum(qty) as order_total from (select k.catalogno, k.distributor, k.entry_date, qty from kdglog as k inner join (select max(entry_date) as max_date, distributor, catalogno from kdglog group by distributor, catalogno) as m on m.max_date = k.entry_date and m.catalogno = k.catalogno and m.distributor = k.distributor where k.entry_type != 'SHIPMENT' and k.entry_type != 'ORDER' order by k.catalogno) as p group by catalogno) as onhand on stock.catalogno = onhand.catalogno inner join (select releaseartist, releasetitle, releasedate, catalogno from catalognew) as cat on stock.catalogno = cat.catalogno;