【发布时间】:2018-12-12 20:48:15
【问题描述】:
我正在尝试在两个表之间进行左外连接。
select id, startdate, name, code, email from edw.dbo.starts
id startdate name code email yearfiled
15 2/4/2018 SO 1083 sql@gmail.com 2018
17 3/4/2018 SO 1083 ssms@gmail.com 2018
19 4/4/2018 SO 1083 ssrs@gmail.com 2018
21 5/4/2018 SO 1083 ssas@gmail.com 2018
21 5/5/2017 SO 1083 who@gmail.com 2017
select customer, return_year, revenue, code from sql.dbo.paid
customer return_year revenue code
15 2018 15.00 1083
17 2018 25.00 1083
21 2018 35.00 1083
21 2017 35.00 1083
select
month(os.startdate) as startmonth
,os.name
,os.code
,coalesce(s.revenue, 0) as revenue
,count(os.email) as commission
from
edw.dbo.starts as os
left outer join
sql.dbo.paid as s
on
os.id = s.customer
and os.yearfile = s.return_year
where
os.yearfiled = 2018
and os.code = '1083'
and os.startdate is not null
group by
month(os.startdate)
,os.name
,os.code
,coalesce(s.revenue, 0);
startmonth name code revenue commission
2 SO 1083 15.00 1
3 SO 1083 25.00 1
4 SO 1083 0.00 1
5 SO 1083 0.00 1
问题:
Customer = 21 来自 sql.dbo.paid 显示连接查询中的收入为零,即使它在表中报告了 35.00 美元的收入。
请求:
startmonth name code revenue commission
2 SO 1083 15.00 1
3 SO 1083 25.00 1
4 SO 1083 0.00 1
5 SO 1083 35.00 1
【问题讨论】:
-
2018 年也有一个。
-
零似乎与记录 19 相关
-
我看不出您的查询有什么问题:rextester.com/XKAAFH1038
-
当我在我的系统上构建相同的表和数据并执行您的查询时,我得到了正确的结果。所以,我不知道你是如何在那一行得到 0 的。
-
只需将客户 ID 添加到您的查询中,您就会看到该行与客户 19 相关联。真正添加 ID
标签: sql sql-server tsql sql-server-2016