I want to extract result based on match from multiple tables, three tables to be specific. The tables are Despatch
, Activation
and Replaced
. Diagram below shows structure and sample data in them.
The Expected result based on above diagram is:
LotQty | ApprovedQty | DispatchDate | Installed
15 | 10 | 2013-8-7 | 9
The result should be on following criteria:
LotQty, ApprovedQty and DispatchDate are just count and distinct from Despatch
table
Installed(Real Problem). This field is based on calculation:
Query should first match
SerialNo
fromDespatch
table for Lot 20 withSerialNo
under Lot 20 inActivation
table. It finds 8 match ignoring Product1&2 which have different Lot quantities.Next for 8 matched SerialNo's it should compare date. If
DispatchDate
for aSerialNo
is less thanActivationDate
then it is count as 1.(Product3'sDispatchDate
is greater thanActivationDate
. Hence it is ignored. For rest of the SerialNo'sDispatchDate
is <ActivationDate
). So total resultant count fromActivation
table is 7.For unmatched SerialNo's from
Despatch
table (Product1,2 & 3) it should perform a match inReplaced
table. It finds match for all the SerialNo's but as Product4 to Product10 are already matched fromActivation
, here it should match only 3 leftover SerialNo's(Product1,2 & 3). Product1 & 2 matches here. Next similar to above it should check ifDispatchDate
is <RecordDate
. Both Product1 & 2 haveDispatchDate
<RecordDate
so count fromReplaced
table is 2.- Now the total
Installed
should be 9 (7 fromActivation
table and 2 fromReplaced
table).
There can be multiple entries of a SerialNo
from Desptach
in both Replaced
and Activation
table as observed in above diagram . So first the match of SerialNo's of Despatch
table should be done with Activation
table and if any of SerialNo
is unmatched or it's DispatchDate
> ActivationDate
only then for those SerialNo's it should go ahead to find a match in Replaced
table.
So far with the help of @ jpw I have managed to form a query as below :
select
(
select distinct LOTQty
from Despatch
where LotQty = '15'
)as LotQty
,(
select COUNT(SerialNo)
from Despatch
where LotQty = '15'
)as ApprovedQty
,(
select distinct(DispatchDate)
from Despatch
where LotQty = '15'
)as DispatchDate
,(
select COUNT(A.SerialNo) + count(R.NewSerialNo)
from Despatch D
left join
Activation A
on D.SerialNo = A.SerialNo
and D.DispatchDate <= A.ActivationDate
and D.LotQty = A.LotQty
left join
Replaced R
on D.SerialNo = R.NewSerialNo
and D.DispatchDate <= R.RecordDate
)as Installed
which yields wrong output i.e :
LotQty | ApprovedQty | DispatchDate | Installed
15 | 10 | 2013-8-7 | 17
Help is deeply appreciated, Thanks in advance
EIDT1
So from above diagram even now the Installed
should be 9 as I would like result only for Lot15
The problem is with your Replaced data. Looking at your condition, it's matching all of the records that are in that table that match the serial number in the Dispatch table. You indicate it should only be 2 though. You are missing a condition that would indicate that it's unmatched in Activation to narrow the results.
Ensuring that A's SerialNo in the second join should do the trick for you
I can't seem to login to SQLFiddle right now, so here is the schema you need if you want to try it out.