I created a temp table
select ol.order_no,ol. date_created,ol .item_id,ol.inv _mast_uid,ol.oe _hdr_uid
INTO #temp
from p21_view_oe_lin e ol
where datediff(dd,ol. date_created,ge tdate()) = 7 and
ol.source_loc_i d=108843
I have info for last 7 days..
I am trying to generate a report that will be results which will give me the order no for a sale if the sales margin was lower then previously sold.
select distinct oh.taker,oh.ord er_no,ad.name as 'Customer name',m.item_id ,
oh.order_date,
case when oel.extended_pr ice = 0 then 0
else (extended_price - commission_cost ) / oel.extended_pr ice end as 'Margin%'
--case when oh.order_date = 0 then 0
--else datediff(dd,oel .date_created,g etdate())< (oh.order_date end as 'Previous Order Date'
--(select date_created
--from #temp as [Previous Order Date]
--where datediff(dd,oel .date_created,g etdate()) =1
--)
from p21_view_oe_hdr oh
left join p21_view_oe_lin e oel on oel.order_no=oh .order_no
left join p21_view_addres s ad on ad.id=oh.custom er_id
left join p21_view_inv_ma st m on m.inv_mast_uid= oel.inv_mast_ui d
left join #temp t on t.date_created= oel.date_create d
and m.inv_mast_uid= t.inv_mast_uid
and t.item_id=oel.i tem_id
and t.date_created < oel.date_create d and t.oe_hdr_uid=oh .oe_hdr_uid
where oh.location_id= 108843
--datediff(dd,oel .date_created,g etdate())< oh.order_date as
--and oel.date_create d < getdate ()
order by 1
the abover query gives me the new orders but i need to add code where i can find if there was a previous order ?
select ol.order_no,ol. date_created,ol .item_id,ol.inv _mast_uid,ol.oe _hdr_uid
INTO #temp
from p21_view_oe_lin e ol
where datediff(dd,ol. date_created,ge tdate()) = 7 and
ol.source_loc_i d=108843
I have info for last 7 days..
I am trying to generate a report that will be results which will give me the order no for a sale if the sales margin was lower then previously sold.
select distinct oh.taker,oh.ord er_no,ad.name as 'Customer name',m.item_id ,
oh.order_date,
case when oel.extended_pr ice = 0 then 0
else (extended_price - commission_cost ) / oel.extended_pr ice end as 'Margin%'
--case when oh.order_date = 0 then 0
--else datediff(dd,oel .date_created,g etdate())< (oh.order_date end as 'Previous Order Date'
--(select date_created
--from #temp as [Previous Order Date]
--where datediff(dd,oel .date_created,g etdate()) =1
--)
from p21_view_oe_hdr oh
left join p21_view_oe_lin e oel on oel.order_no=oh .order_no
left join p21_view_addres s ad on ad.id=oh.custom er_id
left join p21_view_inv_ma st m on m.inv_mast_uid= oel.inv_mast_ui d
left join #temp t on t.date_created= oel.date_create d
and m.inv_mast_uid= t.inv_mast_uid
and t.item_id=oel.i tem_id
and t.date_created < oel.date_create d and t.oe_hdr_uid=oh .oe_hdr_uid
where oh.location_id= 108843
--datediff(dd,oel .date_created,g etdate())< oh.order_date as
--and oel.date_create d < getdate ()
order by 1
the abover query gives me the new orders but i need to add code where i can find if there was a previous order ?
Comment