如何从具有相同日期时间的一组记录中选择最新记录?

时间:2019-05-27 09:03:38

标签: sql sql-server sql-server-2016

因此,我有一些试图在图形中显示的记录。当前,如果某些记录具有相同的日期时间,那么它们会在图表中分组,这是我想要的,但是该图显示的是这些记录的总和,而不是最后一条记录,但是我不确定如何实现。

SUM(TAB.Amount) OVER(PARTITION BY TAB.AccountID order by TAB.AccountID, TransactionValueDate, StatementNumber, SequenceNumber, TAB.RecordID) AS [IntraDayBalance]

这是我用来获取这些记录的代码,这些代码在表中可以正常使用,但是我不能像在上面说的那样对记录求和,就可以在图形中使用它。因此,有一种方法可以从该列中获取具有相同日期时间的最后一条记录。

不确定如何设置格式,但这是一个示例。假设我有3条记录(532255.20,2199722.65和1995722.65)具有完全相同的日期时间(16/04/2019 06:41:04)。当前,在该日期时间点,该图向我显示了这些记录的总和(4727700.5‬0),但我真正希望显示的是最后一个记录(1995722.65)。

然后我要问的是如何显示最后一条记录而不是总金额。请记住,我无法更改上面显示的代码行,因为它可以完美地用于原始用途,而不是用于图形,因此图形需要另外一行,我自己也不知道该如何做。任何帮助将不胜感激!

编辑:谢谢您的帮助。我现在感觉像个白痴,但是我无法将这些语句中的任何一个合并到我的代码中。我对T-SQL还是很陌生,仍然在学习如何正确格式化代码以及处理问题的方法,但这是我的SELECT语句,有关如何将这些语句中的任何一个合并到此代码中的任何提示都将非常有帮助!

        ,TAB.AccountID
        ,TAB.Amount
        ,AC.ShortName
        ,FX.FXRate
        ,FX.IsDivide
        ,CASE 
            WHEN FXRate IS NOT NULL AND FXRate <> 0 THEN
                CASE
                    WHEN FX.IsDivide = 0 THEN
                        ROUND(TAB.Amount / FXRate,2)
                    ELSE
                        ROUND(TAB.Amount * FXRate,2)
                END
            ELSE
                0
        END AS BalanceUSD
        ,ITS.LimitAmount
        ,ITS.FloorAmount
        ,CR.SwiftCode AS Ccy
        ,TAB.RecordType
        ,AC.AccountNumber
        ,AC.BankFileIdentifier AS Accountidentifier
        ,DV.ShortName AS Division
        ,CP.ShortName AS Counterparty
        ,TAB.Amount
        ,SUM(TAB.Amount) OVER(PARTITION BY TAB.AccountID  order by TAB.AccountID, TransactionValueDate, StatementNumber, SequenceNumber, TAB.RecordID) AS [IntraDayBalance]
        ,CAST(29218 + TransactionValueDate AS DATETIME) AS TransactionValueDate
        ,CAST(29218 + TransactionEntryDate AS DATETIME) AS TransactionEntryDate
        ,CASE
            WHEN TransactionFlow = 0 THEN
                TransactionAmount
            ELSE
                -TransactionAmount
        END AS TransactionAmount
        ,CASE 
            WHEN ITS.TransactionFlow = 0 THEN
                ITS.TransactionAmount
            ELSE
                0
        END AS Inflow
        ,CASE 
            WHEN ITS.TransactionFlow = 1 THEN
                ITS.TransactionAmount
            ELSE
                0
        END AS Outflow
        ,TransactionFlow
        ,TransactionCode
        ,CustomerReference
        ,BankReference
        ,Right(RTRIM('0000000000' + CAST(StatementNumber AS CHAR (20))), 10) AS StatementNumber
        ,RTRIM(CAST(ITS.SequenceNumber AS CHAR (20))) AS SequenceNumber
        ,CAST(29218 + StatementDate AS DATETIME) AS StatementDate
        --,DATEADD(SECOND,ImportedByTime,CAST(29218 + StatementDate AS DATETIME))  AS StatementDateTime
        ,CASE
            WHEN TAB.RecordType = 'B' THEN
                @StartDate
            ELSE
                DATEADD(SECOND,ImportedByTime,CAST(29218 + StatementDate AS DATETIME))  
        END AS StatementDateTime
        ,InformationToAccountOwner
        ,Information1
        ,Information2
        ,Information3
        ,Information4
        ,Information5
        ,Information6
        ,ImportedBy
        ,CAST(29218 + ImportedByDate AS DATETIME) AS ImportedByDate
        ,CASE
            WHEN TAB.RecordType = 'B' THEN
                @StartDate
            ELSE
                DATEADD(SECOND,ImportedByTime,CAST(29218 + ImportedByDate AS DATETIME))  
        END AS ImportDateTime
        ,@StartDate AS StartDate
        ,@EndDate AS EndDate
        ,ITS.DebitAmount
        ,ITS.CreditAmount
        ,ITS.FloorAmount
        ,ITS.LimitAmount
        ,ITS.RecordID


FROM CTE_TransactionsAndBalances AS TAB

LEFT OUTER JOIN
    InterimTransactionStatement AS ITS ON TAB.RecordID = ITS.RecordID

LEFT OUTER JOIN
    Accounts AS AC ON TAB.AccountID = AC.ACRecordID

LEFT OUTER JOIN
    Currencies AS CR ON AC.CurrencyID = CR.CRRecordID

LEFT OUTER JOIN
    Divisions AS DV ON AC.DivisionID = DV.DVRecordID

LEFT OUTER JOIN
    Counterparties AS CP ON AC.CounterpartyID = CP.CPRecordID

LEFT OUTER JOIN
    @tbl_FXRates AS FX ON AC.CurrencyID = FX.FromCcyID
    AND FXRateDateN >= FX.StartDateN
    AND FXRateDateN <= FX.EndDateN
    AND FX.ToCcyID = @ReportingCcyID

--WHERE AC.ACRecordID = 94
 order by TAB.AccountID, TransactionValueDate, StatementNumber, SequenceNumber, TAB.RecordID ```

2 个答案:

答案 0 :(得分:0)

使用子查询和row_numberr()

select * from ( select
SUM(TAB.Amount) OVER(PARTITION BY TAB.AccountID  order by TAB.AccountID, TransactionValueDate, StatementNumber, SequenceNumber, TAB.RecordID) AS [IntraDayBalance],
row_number() over(partition by TAB.AccountID order by TransactionValueDate desc) rn
from table
) a where a.rn=1

答案 1 :(得分:-1)

 SELECT Amount,
       TDate
 FROM
 (
    SELECT 
         ROW_NUMBER() OVER (PARTITION BY TransactionValueDate ORDER BY TransactionValueDate) AS RNo,
         TAB.Amount AS Amount,
         TransactionValueDate AS TDate
    FROM table_name TAB
) T
WHERE T.RNo = 1

通过使用'TransactionValueDate'进行排序,您将获得以相同的DateTime值分组的行号。

例如

RNo | Amoount |日期

1 | 11500.00 | 2018-01-01

2 | 11700.00 | 2018-01-01

3 | 11000.00 | 2018-01-01

1 | 11000.00 | 2018-01-02

2 | 9000.00 | 2018-01-02

然后您可以使用子查询相应地过滤掉第一条记录。

您也可以使用DENSE_RANK功能。