【问题标题】:How to get the max date from Joined tables in SQL Server 2014?如何从 SQL Server 2014 中的联接表中获取最大日期?
【发布时间】:2017-05-24 21:56:27
【问题描述】:

我有四个要加入的表来获取我需要的一些数据。表格及部分样本数据如下:

**TollTransaction table**       
AccountId   EntryTransDt    LicPlateNo
1655024     24-05-2017          ABC123
1655024     24-05-2017          DEF123
1655024     24-05-2017          GHI123
1655024     24-05-2017          JKL123
1655024     24-05-2017          MNO123


**Plate table**     
AccountId   LicPlateNo  EndDate
11001       ABC123      2012-06-10
1898884     ABC123      NULL
1981834     DEF123      NULL
14066       GHI123      NULL
1770746     JKL123      NULL
1005010     MNO123      NULL


**Account table**       
AccountId   AccountNumber   CurrentBalance
11001       10110014        0
14066       10140668        0
1005010     20050108        0
1770746     27707463        3.9
1898884     28988847        0
1981834     29818345        0


**FinTransMaster table**        
FinTransTypeCode    BusinessDay AcctID
PYMT                03-02-2015  11001
PYMT                15-01-2015  11001
PYMT                11-12-2014  14066
PYMT                11-09-2014  14066
PYMT                01-04-2016  1005010
PYMT                02-10-2014  1005010
PYMT                15-09-2015  1770746
PYMT                30-11-2015  1898884
PYMT                21-10-2015  1898884
PYMT                23-03-2017  1981834

TollTransaction 表中的 AccountId 对于这些车牌是相同的,因为这些车牌遵循一个共同的标准。

我需要从 Plate 表中获取 AccountIds,然后在 AccountIds 上加入 Account 表以​​获取 AccountNumber。

我想做的事:

  1. 我正在尝试查找具有当前付款余额的帐户。
  2. 最后一次付款日期(FinTransMaster 表中的最大 BusinessDay)。
  3. TollTransaction 表中该 LicPlateNo 的最后一个 EntryTransDt。

我的代码如下:

    SELECT A.AccountNumber
          ,A.CurrentBalance
          ,MAX(F.BusinessDay) over(Partition by F.AcctID) as Last_Pymt_date
          ,MAX(T.EntryTransDt) over(Partition by T.LicPlateNo) as Last_Transaction
    FROM TollTransaction T
    INNER JOIN Plate P ON T.LicPlateNo = P.LicPlateNo
    INNER JOIN Account A ON P.AccountId = A.AccountId
    LEFT JOIN FinTransMaster F ON A.AccountId = F.AcctID

    WHERE T.AccountId = '1655024'
      AND P.EndDate IS NULL
      AND A.CurrentBalance > 0
      AND F.FinTransTypeCode = 'PYMT'

    ORDER BY Last_Pymt_Date DESC, A.AccountNumber

但是我的记录太多了。

我的 TollTransactions 表有多个相同 LicPlateNo 的记录。这就是为什么我在 JOIN 之后获得多条记录的原因。如果我只能将 Distinct T.LicPlateNo 加入其他表,我应该得到单个记录。

编辑: 我使用了下面提供的@SQLZim 代码,但我仍然得到一些重复。请参阅下面的部分结果:

AccountNumber   CurrentBalance  Last_Pymt_date  Last_Transaction
1004219         40.33           24-05-2017      23-05-2017
1004219         40.33           24-05-2017      21-05-2017
1004219         40.33           24-05-2017      19-05-2017
1004219         40.33           24-05-2017      26-05-2016
1082215         60.01           24-05-2017      27-03-2017
1043516         181.25          24-05-2017      07-03-2016
1043516         181.25          24-05-2017      24-05-2017
1043516         181.25          24-05-2017      20-05-2017
1043516         181.25          24-05-2017      03-10-2015

我什至在两个地方注释掉了 T.LastTRansaction,以删除该字段。我仍然得到重复。

【问题讨论】:

    标签: sql-server join


    【解决方案1】:

    使用group by 的标准聚合查询,而不是使用窗口函数进行聚合:

    select  A.AccountNumber
          , A.CurrentBalance
          , max(F.BusinessDay) as Last_Pymt_date
          , max(T.EntryTransDt) as Last_Transaction
    from Account A  
      inner join Plate P 
        on P.AccountId = A.AccountId
      inner join tollTransaction T 
        on T.LicPlateNo = P.LicPlateNo
      left join FinTransMaster F 
        on A.AccountId = F.Acctid
    where A.AccountId = '1655024'
      and A.CurrentBalance > 0
      and P.EndDate is null
      and F.FinTransTypeCode = 'pymt'
    group by  
        A.AccountNumber
      , A.CurrentBalance
    

    【讨论】:

    • 谢谢 SqlZim,但我得到了重复的记录。请问你能在我的问题中看到我的编辑吗?
    • 带有 TollTransaction 的 INNER JOINing Plate 将给出多条记录,因为 TollTransaction 表具有相同 LicPlateNo 的多条记录。我只需要将 TollTransaction 中的 Distinct LicPlateNo 与 Plate 表连接起来。
    • @user1777929 我认为你根本不需要LicPlateNo。答案已更新。
    • @user1777929 乐于助人!
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多