左外加入SSMS

时间:2018-12-12 20:48:16

标签: sql sql-server tsql sql-server-2016

我正在尝试在两个表之间保留外部联接。

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

1 个答案:

答案 0 :(得分:0)

请尝试这个。

create table starts
(id int , 
startdate date , 
name varchar(10), 
code int , 
email varchar(20),
yearfiled int )
create table paid
( customer int , 
return_year int , 
revenue decimal(10,2), 
code int) 
insert into starts values 
(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)
insert into paid values 
(15,2018,15.00,1083),
(17,2018,25.00,1083),
(21,2018,35.00,1083),
(21,2017,35.00,1083)

select month(a.startdate)as startmonth,a.name,a.code,
case when b.revenue is null then 0 else b.revenue end as revenue,
count(a.email) as commission
from starts a 
left join paid b on a.yearfiled=b.return_year and a.id=b.customer
where a.yearfiled=2018 and a.code=1083
group by month(a.startdate),a.name,a.code,b.revenue
/*
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
*/