SQL左联接,最新记录

时间:2020-07-03 10:58:00

标签: sql sql-server tsql

我只想让表保留最新记录。 我有Customer1张桌子:

+--------+----------+
| CustID | CustName |
+--------+----------+
|      1 | ABC123   |
|      2 | 456XYZ   |
|      3 | 5PQR3    |
|      4 | 789XYZ   |
|      5 | 789A     |
+--------+----------+

SalesInvoice表:

+------------+--------+-----------+
|  InvDate   | CustID | InvNumber |
+------------+--------+-----------+
| 2020-03-01 |      1 | IV236     |
| 2020-04-07 |      1 | IV644     |
| 2020-06-13 |      2 | IV869     |
| 2020-03-29 |      3 | IV436     |
| 2020-02-06 |      3 | IV126     |
+------------+--------+-----------+

我想要这个必需的输出:

+--------+------------+-----------+
| CustID |  InvDate   | InvNumber |
+--------+------------+-----------+
|      1 | 2020-04-07 | IV644     |
|      2 | 2020-06-13 | IV869     |
|      3 | 2020-03-29 | IV436     |
|      4 |            |           |
|      5 |            |           |
+--------+------------+-----------+

为方便快捷,下面是示例代码。

    drop table if exists #Customer1
    create table #Customer1(CustID int, CustName varchar (100))
    insert into #Customer1 values
    (1,'ABC123'),
    (2,'456XYZ'),
    (3,'5PQR3'),
    (4,'789XYZ'),
    (5,'789A')
    
    
    drop table if exists #SalesInvoice
    create table #SalesInvoice(InvDate DATE, CustID INT, InvNumber varchar (100))
    insert into #SalesInvoice values
    ('2020-03-01',1,'IV236'),
    ('2020-04-07',1,'IV644'),
    ('2020-06-13',2,'IV869'), 
    ('2020-03-29',3,'IV436'),
    ('2020-02-06',3,'IV126')

3 个答案:

答案 0 :(得分:3)

在这种情况下,我喜欢使用TOP 1 WITH TIES

SELECT TOP 1 WITH TIES c.CustID, i.InvDate, i.InvNumber
FROM #Customer1 c
LEFT JOIN #Invoices i ON c.CustID = i.CustID
ORDER BY ROW_NUMBER() OVER (PARTITION BY c.CustID ORDER BY i.InvDate DESC);

screen capture from demo link below

Demo

这里最重要的1个技巧是按行号排序,为每个客户分配一个顺序,该顺序按发票日期降序。然后,此方法仅保留每个客户的最新发票记录。

答案 1 :(得分:2)

我推荐outer apply

select c.*, i.*
from #c c outer apply
     (select top (1) i.*
      from #invoices i
      where i.custId = c.custId
      order by i.invDate desc
     ) i;

outer apply实现了一种特殊的join类型,称为“横向连接”。这是一个非常强大的构造。但是,在学习它们时,您可以将横向联接视为可以返回多于一列和多于一行的相关子查询。

答案 2 :(得分:0)

您可以使用此简单的自解释性T-SQL尝试ROW_NUMBER窗口函数而不是横向联接

SELECT      c.CustID
            , d.InvDate
            , d.InvNumber
FROM        #C c
LEFT JOIN   (
            SELECT  *
                    , ROW_NUMBER() OVER (PARTITION BY CustID ORDER BY InvDate DESC) AS RowNo
            FROM    #D
            ) d
ON          c.CustID = d.CustID
            AND d.RowNo = 1

基本上,ROW_NUMBER用于在一次表扫描中过滤“最后”发票,而不是在相关查询中执行SELECT TOP 1 ... ORDER BY,该查询必须多次执行-与客户数量一样多