如何使用 Max(date) 获取记录,然后比较值以获得结果

How do I get record with Max(date) and then compare values to get result

我正在尝试获取每个 MeterNumber 的 Max(StartDate) 记录,然后尝试获取特定 AccountNumber 的开始日期、结束日期和 RateCode 不同的记录。

示例数据脚本如下

create table Meter 
(
    AccountNumer varchar(50), 
    MeterNumber varchar(50), 
    StartDate date, 
    EndDate date, 
    RateCode Varchar(50)
)

Insert into Meter Values('0142628117','123470203','4/22/2020','12/31/9999','UGE-PECO-E-R-V-GRN-HOLDOVER-ETF0_APR20')
Insert into Meter Values('0142628117','123470203','4/10/2019', '4/9/2020', '***Custom***')
Insert into Meter Values('0142628117','123470205','4/22/2020','12/31/9999','UGE-PECO-E-R-V-GRN-HOLDOVER-ETF0_APR20')
Insert into Meter Values('0142628117','123470205','4/10/2019', '4/9/2020', '***Custom***')
    
Insert into Meter Values('0500000178767001363445','TCA105238304','02/25/2016','04/22/2016', '***Custom***')
Insert into Meter Values('0500000178767001363445','TCA105238304','10/2/2018','08/11/2019', '***Custom***')
Insert into Meter Values('0500000178767001363445','TCA130359929','8/12/2019','12/31/9999', '***Custom***')

RowNo   AccountNumer            MeterNumber     StartDate   EndDate     RateCode
    1   0142628117              123470203       2020-04-22  9999-12-31  UGE-PECO-E-R-V-GRN-HOLDOVER-ETF0_APR20
    2   0142628117              123470203       2019-04-10  2020-04-09  ***Custom***
    3   0142628117              123470205       2020-04-22  9999-12-31  UGE-PECO-E-R-V-GRN-HOLDOVER-ETF0_APR20
    4   0142628117              123470205       2019-04-10  2020-04-09  ***Custom***
    5   0500000178767001363445  TCA105238304    2016-02-25  2016-04-22  ***Custom***
    6   0500000178767001363445  TCA105238304    2018-10-02  2019-08-11  ***Custom***
    7   0500000178767001363445  TCA130359929    2019-08-12  9999-12-31  ***Custom***

首先,我需要为特定 AccountNumber 的每个 MeterNumber 找到 Max(StartDate)。输出应该是这样的:

    RowNo   AccountNumer            MeterNumber        StartDate   EndDate     RateCode
        1   0142628117              123470203       2020-04-22  9999-12-31  UGE-PECO-E-R-V-GRN-HOLDOVER-ETF0_APR20
        3   0142628117              123470205       2020-04-22  9999-12-31  UGE-PECO-E-R-V-GRN-HOLDOVER-ETF0_APR20
        6   0500000178767001363445  TCA105238304    2018-10-02  2019-08-11  ***Custom***
        7   0500000178767001363445  TCA130359929    2019-08-12  9999-12-31  ***Custom***

然后我试图从(两行)中获取帐号的开始日期、结束日期和费率代码 不同 的记录。所以我期望的最终结果如下 table.

   RowNo   AccountNumer            MeterNumber        StartDate   EndDate     RateCode
       6    0500000178767001363445  TCA105238304    2018-10-02  2019-08-11  ***Custom***
       7    0500000178767001363445  TCA130359929    2019-08-12  9999-12-31  ***Custom***

任何帮助将不胜感激!

这是一个 3 步过程,首先使用 ROW_NUMBER()

对每个 Account/Meter 组合的记录进行排名
SELECT *,
      RowNumber = ROW_NUMBER() OVER(PARTITION BY AccountNumber, MeterNumber 
                                    ORDER BY EndDate DESC)
FROM Meter

输出

AccountNumber MeterNumber StartDate EndDate RateCode RowNumber
0142628117 123470203 2020-04-22 9999-12-31 ETF0_APR20 1
0142628117 123470203 2019-04-10 2020-04-09 ***Custom*** 2
0142628117 123470205 2020-04-22 9999-12-31 ETF0_APR20 1
0142628117 123470205 2019-04-10 2020-04-09 ***Custom*** 2
1363445 105238304 2018-10-02 2019-08-11 ***Custom*** 1
1363445 105238304 2016-02-25 2016-04-22 ***Custom*** 2
1363445 130359929 2019-08-12 9999-12-31 ***Custom*** 1

N.B。一些数据被缩短以更好地显示

然后您可以过滤 were RowNumber = 1 以获得每个计量表的最新结束日期。

接下来您需要计算不同的 EndDate/RateCode 组合,您不能在窗口函数中使用 COUNT(DISTINCT ...),但是您可以使用 DENSE_RANK():[=19= 来模拟它]

SELECT *,
        CntDistinct = DENSE_RANK() OVER(PARTITION BY AccountNumber 
                                                ORDER BY EndDate, RateCode) 
                        + DENSE_RANK() OVER(PARTITION BY AccountNumber 
                                            ORDER BY EndDate DESC, RateCode DESC) - 1
FROM (  SELECT *,
                RowNumber = ROW_NUMBER() OVER(PARTITION BY AccountNumber, MeterNumber 
                                                ORDER BY EndDate DESC)
        FROM Meter AS m
    ) AS m
WHERE m.RowNumber = 1;

输出

AccountNumber MeterNumber StartDate EndDate RateCode CntDistinct
0142628117 123470203 2020-04-22 9999-12-31 ETF0_APR20 1
0142628117 123470205 2020-04-22 9999-12-31 ETF0_APR20 1
1363445 130359929 2019-08-12 9999-12-31 ***Custom*** 2
1363445 105238304 2018-10-02 2019-08-11 ***Custom*** 2

N.B。一些数据被缩短以更好地显示

最后将所有这些放入进一步的子查询中,并限制在有多个 EndDate/RateCode:

的唯一组合的地方
SELECT AccountNumber, MeterNumber, StartDate, EndDate, RateCode
FROM (  SELECT *,
                CntDistinct = DENSE_RANK() OVER(PARTITION BY AccountNumber 
                                                    ORDER BY EndDate, RateCode) 
                                + DENSE_RANK() OVER(PARTITION BY AccountNumber 
                                                    ORDER BY EndDate DESC, RateCode DESC) - 1
            FROM (  SELECT *,
                            RowNumber = ROW_NUMBER() OVER(PARTITION BY AccountNumber, MeterNumber 
                                                        ORDER BY EndDate DESC)
                    FROM Meter AS m
                ) AS m
            WHERE m.RowNumber = 1
    ) AS m
WHERE m.CntDistinct > 1;

输出

AccountNumber MeterNumber StartDate EndDate RateCode
0500000178767001363445 TCA105238304 2018-10-02 2019-08-11 ***Custom***
0500000178767001363445 TCA130359929 2019-08-12 9999-12-31 ***Custom***

Example on db<>Fiddle