如何从SQL Server 2014中的一组连接表中获取max(date)?

时间:2017-05-24 18:41:42

标签: sql-server maxdate

我正在加入四个表来获取我需要的一些数据。表格和一些样本数据如下:

**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)该LicPlateNo的TollTransaction表中的最后一个EntryTransDt。

我的代码如下:

SELECT A.AccountNumber
      ,A.CurrentBalance
      ,(SELECT MAX(BusinessDay) FROM FinTransMaster F WHERE A.AccountId = F.AcctID AND FinTransTypeCode = 'PYMT' GROUP BY AcctID) Last_Pymt_Date
      ,(SELECT MAX(EntryTransDt) FROM TollTransaction GROUP BY LicPlateNo) Last_Transaction

FROM TollTransaction T
INNER JOIN Plate P ON T.LicPlateNo = P.LicPlateNo
INNER JOIN Account A ON P.AccountId = A.AccountId
--INNER JOIN FinTransMaster F ON A.AccountId = F.AcctID

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

ORDER BY Last_Pymt_Date DESC, A.AccountNumber

我的代码在两个SELECT MAX()查询中是错误的。

我需要使用哪些代码来获取最后的日期?

修改 我使用了下面答案中建议的代码但得到的记录太多了。我使用的代码是:

SELECT A.AccountNumber
      ,A.CurrentBalance
      --,(SELECT MAX(BusinessDay) FROM FinTransMaster F WHERE A.AccountId = F.AcctID AND FinTransTypeCode = 'PYMT' GROUP BY AcctID) Last_Pymt_Date
      --,(SELECT MAX(EntryTransDt) FROM TollTransaction GROUP BY LicPlateNo) Last_Transaction
      ,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

ORDER BY Last_Pymt_Date DESC, A.AccountNumber

但我的记录太多了。

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

1 个答案:

答案 0 :(得分:0)

您可以尝试使用如下连接:

SELECT A.AccountNumber
      ,A.CurrentBalance
      --,(SELECT MAX(BusinessDay) FROM FinTransMaster F WHERE A.AccountId = F.AcctID AND FinTransTypeCode = 'PYMT' GROUP BY AcctID) Last_Pymt_Date
      --,(SELECT MAX(EntryTransDt) FROM TollTransaction GROUP BY LicPlateNo) Last_Transaction
      ,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

ORDER BY Last_Pymt_Date DESC, A.AccountNumber