SQL 语句在使用不带 WHERE 子句的外部联接时忽略我的日期条件
SQL statement ignores my date condition when using outer join without a WHERE clause
当没有值时,我使用 outer join
来空白行,但为什么它忽略该行并获取所有日期
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
如果我将 AND
更改为 WHERE
它可以工作但是我丢失了我需要的清零行
--DAILY
SELECT
a.[Name] AS RepName,
CONVERT(VARCHAR, a.TradeDate, 107) AS TimePeriod,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.Quantity * -1
ELSE t.Quantity
END), 0) AS Quantity,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.Principal * -1
ELSE t.Principal
END), 0) AS Principal,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.TradeConcession * -1
ELSE t.TradeConcession
END), 0) AS Comm,
1 AS TheOrder
FROM
dayreps a
LEFT JOIN
Trades t ON a.TradeDate = t.TradeDate
AND a.RepID = t.RepID
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
GROUP BY
a.[Name], a.TradeDate
我相信是通过 LEFT JOIN 进行的,因为 LEFT JOIN 从第一个 table 开始获取所有记录,如果有匹配则独立,在这种情况下第一个 table 是 dayreps。
尝试使用内部联接
SELECT
a.[Name] AS RepName,
CONVERT(VARCHAR, a.TradeDate, 107) AS TimePeriod,
ISNULL(SUM(CASE t.CancelCode
WHEN '1'
THEN t.Quantity * -1
ELSE t.Quantity
END), 0) AS Quantity,
ISNULL(SUM(CASE t.CancelCode
WHEN '1'
THEN t.Principal * -1
ELSE t.Principal
END), 0) AS Principal,
ISNULL(SUM(CASE t.CancelCode
WHEN '1'
THEN t.TradeConcession * -1
ELSE t.TradeConcession
END), 0) AS Comm,
1 AS TheOrder
FROM dayreps a
INNER JOIN Trades t ON a.TradeDate = t.TradeDate and a.RepID = t.RepID
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
GROUP BY a.[Name],
a.TradeDate
勾选这个What is the difference between "INNER JOIN" and "OUTER JOIN"?
PD:抱歉我的英语不好,我在这工作=)
通过在 OUTER JOIN
的 ON
子句中包含条件,您是说如果不匹配此条件,则无法从左侧 table 排除行(dayreps
).相反,这些行是 NULL,就像其他连接条件失败的行一样。
所以原则上将条件移动到WHERE
子句是正确的,但你也必须注意条件说的是什么。如果你把
t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
在 WHERE
子句中,如果日期超出范围, 或 NULL
- t.TransDate
将用于任何被外连接设为 NULL 的行。
幸运的是,您可以改用
a.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
并且,由于它来自您的左侧 table,因此将其放在 WHERE
子句中不会阻碍外部连接。
当没有值时,我使用 outer join
来空白行,但为什么它忽略该行并获取所有日期
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
如果我将 AND
更改为 WHERE
它可以工作但是我丢失了我需要的清零行
--DAILY
SELECT
a.[Name] AS RepName,
CONVERT(VARCHAR, a.TradeDate, 107) AS TimePeriod,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.Quantity * -1
ELSE t.Quantity
END), 0) AS Quantity,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.Principal * -1
ELSE t.Principal
END), 0) AS Principal,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.TradeConcession * -1
ELSE t.TradeConcession
END), 0) AS Comm,
1 AS TheOrder
FROM
dayreps a
LEFT JOIN
Trades t ON a.TradeDate = t.TradeDate
AND a.RepID = t.RepID
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
GROUP BY
a.[Name], a.TradeDate
我相信是通过 LEFT JOIN 进行的,因为 LEFT JOIN 从第一个 table 开始获取所有记录,如果有匹配则独立,在这种情况下第一个 table 是 dayreps。
尝试使用内部联接
SELECT
a.[Name] AS RepName,
CONVERT(VARCHAR, a.TradeDate, 107) AS TimePeriod,
ISNULL(SUM(CASE t.CancelCode
WHEN '1'
THEN t.Quantity * -1
ELSE t.Quantity
END), 0) AS Quantity,
ISNULL(SUM(CASE t.CancelCode
WHEN '1'
THEN t.Principal * -1
ELSE t.Principal
END), 0) AS Principal,
ISNULL(SUM(CASE t.CancelCode
WHEN '1'
THEN t.TradeConcession * -1
ELSE t.TradeConcession
END), 0) AS Comm,
1 AS TheOrder
FROM dayreps a
INNER JOIN Trades t ON a.TradeDate = t.TradeDate and a.RepID = t.RepID
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
GROUP BY a.[Name],
a.TradeDate
勾选这个What is the difference between "INNER JOIN" and "OUTER JOIN"?
PD:抱歉我的英语不好,我在这工作=)
通过在 OUTER JOIN
的 ON
子句中包含条件,您是说如果不匹配此条件,则无法从左侧 table 排除行(dayreps
).相反,这些行是 NULL,就像其他连接条件失败的行一样。
所以原则上将条件移动到WHERE
子句是正确的,但你也必须注意条件说的是什么。如果你把
t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
在 WHERE
子句中,如果日期超出范围, 或 NULL
- t.TransDate
将用于任何被外连接设为 NULL 的行。
幸运的是,您可以改用
a.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
并且,由于它来自您的左侧 table,因此将其放在 WHERE
子句中不会阻碍外部连接。