SQL Union For Existing Query -
i have following query display sales during period of time, unfortunately seems not reporting customers have purchased in other categories well.
for example, customer had never purchased products before (cat 2 or cat 3), , purchased products on 5/5 first time. sale not coming on report because purchased cat 60 previously.
select customer_del_hist.customer_number ,max ( customer_del_hist.serve_location) serve_location ,max ( customer_del_hist.product_number) product_number ,max ( customer_del_hist.del_date) del_date ,max ( order_rte_bal.warehouse) warehouse ,max ( order_rte_bal.route_number) route_number ,max ( order_rte_bal.route_day) route_day customer_del_hist inner join inv_master on customer_del_hist.product_number = inv_master.product_number inner join order_header on customer_del_hist.order_number = order_header.order_number inner join order_rte_bal on order_header.warehouse_number = order_rte_bal.warehouse , order_header.route_number = order_rte_bal.route_number , order_header.route_day = order_rte_bal.route_day , order_header.delivery_date = order_rte_bal.route_date (customer_del_hist.del_date between x , y) , (inv_master.inventory_category in ('02', '03', '60', '74')) , (customer_del_hist.customer_number not in (select h2.customer_number customer_del_hist h2 inner join inv_master on h2.product_number = i.product_number h2.del_date between '6/01/2014' , '04/30/2015' , i.inventory_category in ('02', '03', '60', '74'))) group customer_del_hist.customer_number
i'm new sql know should use union in select, that's it. unfortunately i'm not versed enough, looking how can go getting results want.
can't offer in way of helping real problem since said new thought show how cleaner query can tiny bit of effort. if start using aliases can eliminate dozens of characters per line , code can lot cleaner. lot of formatting personal preference code posted way challenging read. here 1 way utilize aliases , clean considerably.
select cdh.customer_number , max(cdh.serve_location) serve_location , max(cdh.product_number) product_number , max(cdh.del_date) del_date , max(orb.warehouse) warehouse , max(orb.route_number) route_number , max(orb.route_day) route_day customer_del_hist cdh inner join inv_master im on cdh.product_number = im.product_number inner join order_header oh on cdh.order_number = oh.order_number inner join order_rte_bal orb on oh.warehouse_number = orb.warehouse , oh.route_number = orb.route_number , oh.route_day = orb.route_day , oh.delivery_date = orb.route_date cdh.del_date between @startdate , @enddate , im.inventory_category in ('02', '03', '60', '74') , cdh.customer_number not in ( select h2.customer_number customer_del_hist h2 inner join inv_master on h2.product_number = i.product_number h2.del_date between '6/01/2014' , '04/30/2015' , i.inventory_category in ('02', '03', '60', '74') ) group cdh.customer_number
Comments
Post a Comment