sql - Return 1 result on multiple line join -
i have orders , order lines table , want able select few of records order table temp table. easy, hard part want select temp table when specific product used in "order". query wrote, can't use returns multiple results each order depending on how many order lines exist. can 1 help? want see whether order included specific or number of specific produts:
declare @begin_date datetime; set @begin_date = '2015-04-01' declare @end_date datetime; set @end_date = '2015-06-30' select o.location_code, o.order_number, o.order_date, o.orderidealfoodcost o.orderfinalprice, case when ol.productcode in ('172352','172353','172355','172357','172360','172422','172429','172343','172344','172346','172348','172351','172427','172428') 1 else 0 end promo #ordercostprice order_lines ol (nolock) inner join orders o (nolock) on ol.location_code = o.location_code , ol.order_date = o.order_date , ol.order_number = o.order_number o.order_date between @begin_date , @end_date , o.order_status_code <= 98 , ol.deleted = 0
orders table:
location_code|order_number|order_date|orderidealfoodcost|orderfinalprice|order_status_code 30542 |1 |2015-01-01| 5.21 | 21.25 |4 30548 |1 |2015-01-01| 8.25 | 58.26 |4
order lines table:
location_code|order_number|order_date|order_line_number|order_product|deleted 30542 |1 |2015-01-01|1 |172352 |0 30542 |1 |2015-01-01|2 |72352 |0 30542 |1 |2015-01-01|3 |72355 |0 30542 |1 |2015-01-01|4 |72358 |0 30548 |1 |2015-01-01|1 |72352 |0 30548 |1 |2015-01-01|2 |72358 |0 30548 |1 |2015-01-01|3 |72359 |0
desired result:
location_code|order_date|order_number|orderidealfoodcost|orderfinalprice|promo 30542 |2015-01-01|1 |5.21 |21.25 |1 30542 |2015-01-01|1 |8.25 |58.26 |0
any ideas?
if getting results order , flagging if exists include check in select statement below:
declare @begin_date datetime; set @begin_date = '2015-01-01' declare @end_date datetime; set @end_date = '2015-01-30' --insert #ordercostprice select o.location_code, o.order_number, o.order_date, o.orderidealfoodcost, o.orderfinalprice, promo = case when ( select count(*) order_lines location_code = o.location_code , order_date = o.order_date , order_number = o.order_number , order_product in ('172352','172353','172355','172357','172360','172422','172429','172343','172344','172346','172348','172351','172427','172428') ) > 0 1 else 0 end orders o (nolock) o.order_date between @begin_date , @end_date , o.order_status_code <= 98
Comments
Post a Comment