MCTampa
Board Regular
- Joined
- Apr 14, 2016
- Messages
- 97
I have the following two tables:
Automatics with Dates
Inventory Count
And have written the following query to determine the quantity of units from table 2 which fall within the ranges outlined on table 1:
However my result is as follows:
The issue is that I'm seeing 932 units for VIT Size 2 between both 8/13/2020 and 9/30/2020 (which is correct) AND between 12/10/2020 and 12/16/2020 (which is not).
Why is my query applying the same total to both ranges of dates?
Query1
Automatics with Dates
RAREA | RCODE | RRATCD | Size | StartWeek | EndWeek | Start Date | End Date | Start Year | End Year |
---|---|---|---|---|---|---|---|---|---|
330 | VIT | A | 1 | 34 | 39 | 8/20/2020 | 9/30/2020 | 2020 | 2020 |
330 | VIT | A | 1 | 50 | 50 | 12/10/2020 | 12/16/2020 | 2020 | 2020 |
330 | VIT | A | 2 | 33 | 39 | 8/13/2020 | 9/30/2020 | 2020 | 2020 |
330 | VIT | A | 2 | 50 | 50 | 12/10/2020 | 12/16/2020 | 2020 | 2020 |
RESX | EXDTFDTF | UCDX | CountOfEXCH# |
---|---|---|---|
VIT | 8/28/2020 | 2 | 71 |
VIT | 8/29/2020 | 2 | 60 |
VIT | 8/30/2020 | 2 | 9 |
VIT | 9/4/2020 | 2 | 63 |
VIT | 9/5/2020 | 2 | 63 |
VIT | 9/6/2020 | 2 | 8 |
VIT | 9/11/2020 | 2 | 49 |
VIT | 9/12/2020 | 2 | 64 |
VIT | 9/13/2020 | 2 | 9 |
VIT | 9/18/2020 | 2 | 49 |
VIT | 9/19/2020 | 2 | 72 |
VIT | 8/14/2020 | 2 | 71 |
VIT | 8/15/2020 | 2 | 57 |
VIT | 8/16/2020 | 2 | 7 |
VIT | 8/21/2020 | 2 | 70 |
VIT | 8/22/2020 | 2 | 59 |
VIT | 8/23/2020 | 2 | 8 |
VIT | 9/20/2020 | 2 | 9 |
VIT | 9/25/2020 | 2 | 51 |
VIT | 9/26/2020 | 2 | 73 |
VIT | 9/27/2020 | 2 | 10 |
| | | 932 |
VBA Code:
SELECT
[Automatics with Dates].RAREA,
[Automatics with Dates].RCODE,
[Automatics with Dates].RRATCD,
[Automatics with Dates].Size,
[Automatics with Dates].[Start Date],
[Automatics with Dates].[End Date],
[Automatics with Dates].StartWeek,
[Automatics with Dates].EndWeek,
Sum([Inventory Count].[CountOfEXCH#]) AS [Units]
FROM [Automatics with Dates] LEFT JOIN [Inventory Count] ON ([Automatics with Dates].RCODE = [Inventory Count].RESX) AND ([Automatics with Dates].Size = [Inventory Count].UCDX)
WHERE ((([Automatics with Dates].RCODE)='VIT')
AND ((Exists (SELECT *
FROM [Automatics with Dates] ab
WHERE ab.RCODE = [Inventory Count].RESX
AND ab.Size = [Inventory Count].UCDX
AND [Inventory Count].EXDTFDTF >= ab.[Start Date]
AND [Inventory Count].EXDTFDTF <= ab.[End Date]
))<>False))
GROUP BY [Automatics with Dates].RAREA, [Automatics with Dates].RCODE, [Automatics with Dates].RRATCD, [Automatics with Dates].Size, [Automatics with Dates].[Start Date], [Automatics with Dates].[End Date], [Automatics with Dates].StartWeek, [Automatics with Dates].EndWeek;
However my result is as follows:
The issue is that I'm seeing 932 units for VIT Size 2 between both 8/13/2020 and 9/30/2020 (which is correct) AND between 12/10/2020 and 12/16/2020 (which is not).
Why is my query applying the same total to both ranges of dates?
Query1
RAREA | RCODE | RRATCD | Size | Start Date | End Date | StartWeek | EndWeek | Units |
---|---|---|---|---|---|---|---|---|
330 | VIT | A | 1 | 8/20/2020 | 9/30/2020 | 34 | 39 | 11 |
330 | VIT | A | 1 | 12/10/2020 | 12/16/2020 | 50 | 50 | 11 |
330 | VIT | A | 2 | 8/13/2020 | 9/30/2020 | 33 | 39 | 932 |
330 | VIT | A | 2 | 12/10/2020 | 12/16/2020 | 50 | 50 | 932 |
| | | | | | | | |