2017-05-24 58 views
1

我有四張表,我正在加入以獲取我需要的一些數據。是表和一些示例數據如下:如何從SQL Server 2014中的連接表中獲取最大日期?

**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 
      ,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,以刪除該字段。我仍然得到重複。

回答

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 
+0

謝謝SqlZim,但我發現重複的記錄。請你能在我的問題中看到我的編輯嗎? – user1777929

+0

帶TollTransaction的INNER JOINing Plate會給出多條記錄,因爲TollTransaction表對同一個LicPlateNo有多條記錄。我需要通過TollTransaction與Plate表只連接Distinct LicPlateNo。 – user1777929

+0

@ user1777929我不認爲你需要'LicPlateNo'。答案已更新。 – SqlZim

相關問題