Sql 右外部联接未返回预期行?
我正在尝试使用以下连接两个表的SQL Server查询为预测接口返回.NET datatable:Sql 右外部联接未返回预期行?,sql,sql-server,Sql,Sql Server,我正在尝试使用以下连接两个表的SQL Server查询为预测接口返回.NET datatable: SELECT accounts.AccountID ,accounts.Account ,SUM(CASE WHEN forecast.omonth = 1 THEN forecast.ns1 ELSE 0 END) / 1 Jan ,SUM(CASE WHEN forecast.omonth = 2
SELECT accounts.AccountID
,accounts.Account
,SUM(CASE
WHEN forecast.omonth = 1
THEN forecast.ns1
ELSE 0
END) / 1 Jan
,SUM(CASE
WHEN forecast.omonth = 2
THEN forecast.ns1
ELSE 0
END) / 1 Feb
,SUM(CASE
WHEN forecast.omonth = 3
THEN forecast.ns1
ELSE 0
END) / 1 Mar
,SUM(CASE
WHEN forecast.omonth = 4
THEN forecast.ns1
ELSE 0
END) / 1 Apr
,SUM(CASE
WHEN forecast.omonth = 5
THEN forecast.ns1
ELSE 0
END) / 1 May
,SUM(CASE
WHEN forecast.omonth = 6
THEN forecast.ns1
ELSE 0
END) / 1 Jun
,SUM(CASE
WHEN forecast.omonth = 7
THEN forecast.ns1
ELSE 0
END) / 1 Jul
,SUM(CASE
WHEN forecast.omonth = 8
THEN forecast.ns1
ELSE 0
END) / 1 Aug
,SUM(CASE
WHEN forecast.omonth = 9
THEN forecast.ns1
ELSE 0
END) / 1 Sep
,SUM(CASE
WHEN forecast.omonth = 10
THEN forecast.ns1
ELSE 0
END) / 1 Oct
,SUM(CASE
WHEN forecast.omonth = 11
THEN forecast.ns1
ELSE 0
END) / 1 Nov
,SUM(CASE
WHEN forecast.omonth = 12
THEN forecast.ns1
ELSE 0
END) / 1 Dec
FROM accountForecast forecast
RIGHT OUTER JOIN account_tree accounts ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
accountForecast表的结构如下所示:
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
此查询应为WHERE子句(35、36、37等)中的所有帐户ID返回一行,即使给定帐户在accountForecast表中没有条目,在这种情况下,查询应返回0
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
我的查询当前仅返回一行,其中包含已插入accountForecast表中的AccountID。我加入错了吗?我应该换什么
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
编辑:我删除了
forecast.oYear=2014
WHERE子句,它似乎按照预期工作。但是,重要的是,该查询必须输入year参数,因为数据依赖于该参数。是否有任何方法可以在指定年份的同时返回所有帐户行?您的筛选器将排除NULL
值:
WHERE forecast.oYear = 2014
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
您的筛选器排除
空值
:
WHERE forecast.oYear = 2014
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
您的筛选器排除
空值
:
WHERE forecast.oYear = 2014
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
您的筛选器排除
空值
:
WHERE forecast.oYear = 2014
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
很难理解您的数据实际存储在哪里,但我假设所有帐户都存储在
帐户树中?如果是这样,请尝试将从
和连接
子句反转:
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
很难理解您的数据实际存储在哪里,但我假设所有帐户都存储在帐户树中?如果是这样,请尝试将从
和连接
子句反转:
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
很难理解您的数据实际存储在哪里,但我假设所有帐户都存储在帐户树中?如果是这样,请尝试将从
和连接
子句反转:
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
很难理解您的数据实际存储在哪里,但我假设所有帐户都存储在帐户树中?如果是这样,请尝试将从
和连接
子句反转:
FROM account_tree accounts
RIGHT OUTER JOIN accountForecast forecast ON forecast.AccountID = accounts.AccountID
WHERE forecast.oYear = 2014
AND accounts.AccountID in (35, 36, 37, 38, 39, 40, 41, 78, 79, 80, 81, 1, 82, 176)
GROUP BY accounts.AccountID
,accounts.Account
帐户树表是如何设置的?还有,sql的哪个版本?帐户树表是如何设置的?还有,sql的哪个版本?帐户树表是如何设置的?还有,sql的哪个版本?帐户树表是如何设置的?还有,sql的哪个版本?