hanchilicious
Programmer
Hi,
How do I join these two tables accurately?
I have three records in the FORECAST table:
and I want to join them to the correct corresponding records in the SALES table:
Here're the rules:
(a) I never want to assign any records from FORECAST to SALES where the Area = SP20.
(b) I have to support a FULL OUTER JOIN as there may be independent records in either FORECAST or SALES which need to be represented.
(c) In the case where there are multiple Agents per Customer and Product, only one of the Agents will have values in the SALES.WK* columns, and that is the row that should have the FORECAST row linked to it.
The end result should look something like this:
I don't seem to be able to support all three rules at the same time without losing some records from the forecast table... I'm trying full outer joins with subqueries to alienate the agent I need, but either the subqueries complain that I'm returning more than one value, or I'm missing records with multiple agents or records that only exist in one table.
Can someone point me in the right direction?
Thanks a lot!
How do I join these two tables accurately?
I have three records in the FORECAST table:
Code:
Customer Product Wk04 Wk05 Wk06
-----------------------------------
TANDY PLY001 4 3 2
ULYSSES FEV999 5 2 1
NEWGUY RUF101 9
Code:
Customer Product Area Agent Wk01 Wk02 Wk03
------------------------------------------------
TANDY PLY001 GR31 10001 10 11 12
TANDY PLY001 SP20 12302 0 0 0
ULYSSES FEV999 BG27 9999 0 0 0
ULYSSES FEV999 BG27 88882 15 15 15
OLDGUY OLD001 ED11 10101 1 1 1
(a) I never want to assign any records from FORECAST to SALES where the Area = SP20.
(b) I have to support a FULL OUTER JOIN as there may be independent records in either FORECAST or SALES which need to be represented.
(c) In the case where there are multiple Agents per Customer and Product, only one of the Agents will have values in the SALES.WK* columns, and that is the row that should have the FORECAST row linked to it.
The end result should look something like this:
Code:
Customer Product Area Agent Wk01 Wk02 Wk03 Wk04 Wk05 Wk06
------------------------------------------------------------------
TANDY PLY001 GR31 10001 10 11 12 4 3 2
TANDY PLY001 SP20 12302 0 0 0 null null null
ULYSSES FEV999 BG27 9999 0 0 0 null null null
ULYSSES FEV999 BG27 88882 15 15 15 5 2 1
NEWGUY RUF101 null null null null null null null 9
OLDGUY OLD001 ED11 10101 1 1 1 null null null
I don't seem to be able to support all three rules at the same time without losing some records from the forecast table... I'm trying full outer joins with subqueries to alienate the agent I need, but either the subqueries complain that I'm returning more than one value, or I'm missing records with multiple agents or records that only exist in one table.
Can someone point me in the right direction?
Thanks a lot!