Hi, i have a data that came from my main query which i need to manipulate by matching a pair of records using esn1, en3 and it should be matching with pairs of descript CMA Receive and CMA Shipping. if the esn does not have this 2 pairs of descript it should not be included. CMA Receive and CMA shipping, this is a receive transaction from warehouse so its always an equal descript. Could not figure out on how to get the result based on the sample below. below is a sample DDL.
[code]
create table #s1
(esn1 nvarchar(35), esn2 nvarchar(35), esn3 nvarchar(35), prodid nvarchar(35), trndate datetime, operation nvarchar(35), descript nvarchar(35))
insert into #s1
select '354388062058004','354388062058004','NULL','NULL','2016-02-09 01:59:37.093','CMA Receive','CMA Receive' union all
select '354388062058004','354388062058004','NULL','NULL','2016-02-09 01:59:38.947','CMA Shipping','CMA Shipping' union all
select 'B9000000000022698487','B9000000000022698487','354388062058004','PRC006826971','2016-02-11 13:05:39.000','Create Order','Open PO' union all
select 'B9000000000022706825','B9000000000022706825','NULL','NULL','2016-02-09 23:37:40.900','CMA Receive','CMA Receive' union all
select 'B9000000000022706825','B9000000000022706825','NULL','NULL','2016-02-09 23:37:42.560','CMA Shipping','CMA Shipping' union all
select 'B9000000000022706825','B9000000000022706825','358536054597910','PRC006840220','2016-02-13 23:52:26.000','Create Order','Open PO' union all
select 'B9000000000022416383','352006066338378','352006066338378','PRC006721162','2016-01-05 13:45:25.670','CMA Receive','CMA Receive' union all
select 'B9000000000022416383','352006066338378','352006066338378','PRC006721162','2016-01-05 13:45:27.290','CMA Shipping','CMA Shipping' union all
select 'B9000000000022346886','B9000000000022346886','NULL','NULL','2016-01-08 23:36:55.157','Write-Off','Write-Off' union all
select 'B9000000000022346917','359238068445033','359238068445033','PRC006709807','2016-01-19 05:07:10.000','Create Order','Create Order' union all
select 'B9000000000022346917','359238068445033','359238068445033','PRC006709807','2016-01-19 07:55:19.000','Order Complete','Scrap' union all
select 'B9000000000022420604','356986068725332','356986068725332','PRC006766546','2016-01-05 23:15:37.860','CMA Receive','CMA Receive' union all
select 'B9000000000022420604','356986068725332','356986068725332','PRC006766546','2016-01-05 23:15:39.390','CMA Shipping','CMA Shipping' union all
select 'B9000000000022420604','356986068725332','356986068725332','PRC006766546','2016-01-28 12:26:21.000','Create Order','Create Order' union all
select 'B9000000000022420604','356986068725332','356986068725332','PRC006766546','2016-01-29 04:14:46.000','Order Complete','FG' union all
select 'B9000000000022420604','356986068725332','356986068725332','PRC006766546','2016-01-29 14:36:23.853','Shipping','Shipping' union all
select '357991057750878','357991057750878','NULL','NULL','2016-02-08 11:52:01.450','Receive','Receive' union all
select '357991057750878','357991057750878','NULL','NULL','2016-02-08 11:52:29.827','Pass',' Pass'
declare @begdate datetime
declare @enddate datetime
set @begdate ='2016-1-1'
set @enddate =getdate()
select esn1, esn2 , esn3, prodid , trndate, operation, descript
from #s1
where trndate between @begdate and @enddate
and operation in ('CMA Receive','CMA Shipping','Create Order','Order Complete','Shipping')
sample result 1
esn1-------------------------esn2-------------- esn3-------------- prodid--------trndate------------------operation------- descript
354388062058004----------354388062058004------- NULL-------------- NULL----------2016-02-09 01:59:37.093--CMA Receive----- CMA Receive
354388062058004----------354388062058004------- NULL-------------- NULL----------2016-02-09 01:59:38.947--CMA Shipping---- CMA Shipping
B9000000000022698487-----B9000000000022698487-- 354388062058004--- PRC006826971--2016-02-11 13:05:39.000--Create Order---- Open PO
B9000000000022706825-----B9000000000022706825-- NULL-------------- NULL----------2016-02-09 23:37:40.900--CMA Receive----- CMA Receive
B9000000000022706825-----B9000000000022706825-- NULL-------------- NULL----------2016-02-09 23:37:42.560--CMA Shipping---- CMA Shipping
B9000000000022706825-----B9000000000022706825-- 358536054597910--- PRC006840220--2016-02-13 23:52:26.000--Create Order---- Open PO
B9000000000022416383-----352006066338378------- 352006066338378--- PRC006721162--2016-01-05 13:45:25.670--CMA Receive----- CMA Receive
B9000000000022416383-----352006066338378------- 352006066338378--- PRC006721162--2016-01-05 13:45:27.290--CMA Shipping---- CMA Shipping
B9000000000022420604-----356986068725332------- 356986068725332--- PRC006766546--2016-01-05 23:15:37.860--CMA Receive----- CMA Receive
B9000000000022420604-----356986068725332------- 356986068725332--- PRC006766546--2016-01-05 23:15:39.390--CMA Shipping---- CMA Shipping
B9000000000022420604-----356986068725332------- 356986068725332--- PRC006766546--2016-01-28 12:26:21.000--Create Order---- Create Order
B9000000000022420604-----356986068725332------- 356986068725332--- PRC006766546--2016-01-29 04:14:46.000--Order Complete-- FG
B9000000000022420604-----356986068725332------- 356986068725332--- PRC006766546--2016-01-29 14:36:23.853--Shipping-------- Shipping
sample result 2
esn1-------------------------esn2-------------- esn3-------------- prodid--------receipt trndate---------------operation----descript---------po_created----------------po_ended---------------shipped date
354388062058004----------354388062058004------- 354388062058004--- PRC006826971--2016-02-09 01:59:37.093--CMA Receive----- CMA Receive--2016-02-11 13:05:39.00
B9000000000022706825-----B9000000000022706825-- 358536054597910--- PRC006840220--2016-02-09 23:37:40.900--CMA Receive----- CMA Receive--2016-02-13 23:52:26.00
B9000000000022416383-----352006066338378------- 352006066338378--- PRC006721162--2016-01-05 13:45:25.670--CMA Receive----- CMA Receive
B9000000000022420604-----356986068725332------- 356986068725332--- PRC006766546--2016-01-05 23:15:37.860--CMA Receive----- CMA Receive--2016-01-28 12:26:21.000--2016-01-29 04:14:46.000--2016-01-29 14:36:23.853[/code]