SQL查询:仅使用LEFT OR RIGHT或INNER JOIN连接两个表 - 需要NULL值

时间:2018-02-22 17:05:24

标签: sql zoho

要遵守的条款是,我有规定只使用LEFT或RIGHT OR INNER JOIN和GROUP_CONTACT。

我有两张表如下:

收藏表:

LoanID  Transacction-Date   Amount
12345   05/02/17             500
12345   06/02/17             1000

OverdueClollection表:

LoanID  Transaction-Date  Amount
12345   07/02/17          250
12345   09/02/17          900

如果我使用以下查询加入他们,请

SELECT
     c.LoanID,
     date(c.TransactionDate),
     date(d.TransactionDate),
     c.Amount,
     d.Amount FROM Collections c LEFT JOIN Overduecollection d ON c.LoanID  = d.LoanID

我得到以下结果

c.LoanID c.TransactionDate  d.TransactionDate   c.Amount   d.Amount
12345     05 Feb, 2018     09 Feb, 2018         500.0      900.0
12345     05 Feb, 2018     07 Feb, 2018         500.0      250.0
12345     06 Jan, 2018     09 Feb, 2018         1000.0     900.0
12345     06 Jan, 2018     07 Feb, 2018         1000.0     250.0

但我需要的结果如下:

c.LoanID c.TransactionDate) d.TransactionDate   c.Amount    d.Amount
12345    05 Feb, 2018                            500.0  
12345                       09 Feb, 2018                    250.0
12345    06 Jan, 2018                           1000.0  
12345                       07 Feb, 2018                    900.0

是否可以考虑上述条款?如果是这样,它需要做什么?或其他最佳实施方式?

2 个答案:

答案 0 :(得分:0)

你真的在寻找一个联合查询,而不是一个联接。

SELECT
    LoanID = c.LoanID,
    C_TransactionDate = date(c.TransactionDate),
    D_TransactionDate = NULL,
    C_Amount = c.Amount,
    D_Amount = NULL 
FROM 
    Collections c 
UNION ALL
SELECT
    LoanID = d.LoanID,
    C_TransactionDate = NULL,
    D_TransactionDate = date(d.TransactionDate),
    C_Amount = NULL,
    D_Amount = d.Amount 
FROM 
    Overduecollection d 

答案 1 :(得分:0)

你不能使用LoanId进行外连接,因为对于所有记录它们是相同的。相反,你可以在交易日期做一个完整的外部联接,这是不同的,这样你就不需要硬编码NULL

create table #collections
(
    loanId int
    ,transactionDate date
    ,amount int
)

create table #overduecollections
(
    loanId int
    ,transactionDate date
    ,amount int
)

insert into #collections
select 12345,'05/02/17', 500 union all
select 12345,'06/02/17', 1000 union all
select 123456,'07/02/17', 55


insert into #overduecollections
select 12345,'07/02/17', 250 union all
select 12345,'09/02/17', 900


select 
     COALESCE(c.loanId,od.loanId) as loanId,
     c.transactionDate as c_transactionDate,
     od.transactionDate as od_transactionDate,
     c.amount as c_amount,
     od.amount as od_amount
from #collections c
    full join #overduecollections od ON c.transactionDate = od.transactionDate --c.loanId = od.loanId


drop table #collections
drop table #overduecollections

输出:

loanId  c_transactionDate   od_transactionDate  c_amount    od_amount
12345   2017-05-02          NULL                500         NULL
12345   2017-06-02          NULL                1000        NULL
123456  2017-07-02          NULL                55          NULL    
12345   NULL                2017-07-02          NULL        250
12345   NULL                2017-09-02          NULL        900

更新: 联接还应该包括loanId来计算具有匹配日期但不同loanIds

的交易
    select 
     COALESCE(c.loanId,od.loanId) as loanId,
     c.transactionDate as c_transactionDate,
     od.transactionDate as od_transactionDate,
     c.amount as c_amount,
     od.amount as od_amount
from #collections c
    full join #overduecollections od ON c.transactionDate = od.transactionDate and c.loanId = od.loanId