How can I set up this left join so that it does not duplicate my calls in Table2 or my sales in Table3?
Left joining to either Table2 or Table3 returns accurate answers, left joining to both in the same query duplicates - while I have three calls and sales of $444, it returns 6 calls (the three records of Table2 x the two records of Table3) and Sales of $1332.
Using SQL Server Studio 17.
CREATE TABLE #Table1 (
CommercialTime datetime,
NextCommercialTime datetime,
Station varchar(4),
Cost int )
INSERT INTO #Table1 (
CommercialTime,
NextCommercialTime,
Station,
Cost )
VALUES
( '2020-04-06 12:01:00.000',
'2020-04-06 12:15:00.000',
'ZZZZ',
'9999' )
CREATE TABLE #Table2 (
CallTime datetime,
Station varchar(4),
CallCount int )
INSERT INTO #Table2 (
CallTime,
Station,
CallCount )
VALUES
( '2020-04-06 12:02:00.000',
'ZZZZ',
'1' ),
( '2020-04-06 12:05:00.000',
'ZZZZ',
'1' ),
( '2020-04-06 12:07:00.000',
'ZZZZ',
'1' )
CREATE TABLE #Table3 (
SaleTime datetime,
Station varchar(4),
SaleAmount int )
INSERT INTO #Table3 (
SaleTime,
Station,
SaleAmount )
VALUES
( '2020-04-06 12:04:00.000',
'ZZZZ',
'123' ),
( '2020-04-06 12:07:00.000',
'ZZZZ',
'321' )
SELECT
one.Station
, SUM(two.Callcount) as Calls
, SUM(three.SaleAmount) as Sales
FROM #Table1 one WITH(NOLOCK)
LEFT JOIN #Table2 two WITH(NOLOCK) ON one.Station = two.Station
AND two.CallTime between one.CommercialTime and one.NextCommercialTime
LEFT JOIN #Table3 three WITH(NOLOCK) ON one.Station = three.Station
AND three.SaleTime between one.CommercialTime and one.NextCommercialTime
GROUP BY one.Station
--Output:
Station Calls Sales
ZZZZ 6 1332