【问题标题】:SQL Query: Joining of Two tables Using LEFT OR RIGHT OR INNER JOIN only - Need NULL valuesSQL 查询:仅使用 LEFT OR RIGHT OR INNER JOIN 连接两个表 - 需要 NULL 值
【发布时间】:2018-08-02 15:36:27
【问题描述】:

要遵循的条款是,我只能使用 LEFT 或 RIGHT OR INNER JOIN 和 GROUP_CONTACT。

我有两张表如下:

集合表:

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

过期收集表:

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

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

【问题讨论】:

    标签: sql zoho


    【解决方案1】:

    您不能使用 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,以说明日期匹配但loanId不同的交易

        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
    

    【讨论】:

    • 非常感谢,您给了我另一种实现所需输出的方法。再次感谢。
    • @UM1979 也使用这种方法更加动态,您不必对空值进行硬编码如果这对您有用,请接受答案。谢谢
    【解决方案2】:

    您实际上是在寻找联合查询,而不是联接。

    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 
    

    【讨论】:

    • 是的,它按预期工作。感谢您的帮助和指导。
    • 如果这对您有用,请接受答案。谢谢!
    猜你喜欢
    • 2015-08-25
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2015-01-10
    • 2013-05-02
    • 2019-08-09
    • 2018-03-23
    • 2017-05-03
    相关资源
    最近更新 更多