我正在尝试使用查询在重复值开始之前查找列中的最大值。我有三列 - 年、工作编号、小时费率
在某一年(可能会有所不同)之后,费率开始重复。
使用下面的数据,我需要找到每个职位编号的最高费率年份。
期望的结果集:
年份 | 职位编号 | 每小时费率 |
---|---|---|
1998 | 6454 | 48.07765385469215 |
1996 | 1126 | 45.662133671135376 |
2000 | 9196 | 42.02154384293608 |
创建表“my_GRID1_TEST” (“年份”数字(38,0), “职位代码”编号(38,0), “HOURLY_RATE”号(38,15) )
Year Jobnumber hourly_rate
2024 1126 35.582755
2023 1126 35.96506525
2022 1126 36.3511986025
2021 1126 36.74119328852501
2020 1126 37.13508792141026
1999 1126 44.59125280927884
1998 1126 44.94554923034844
1997 1126 45.30250287457605
1996 1126 45.662133671135376
1995 1126 45.662133671135376
1994 1126 45.662133671135376
2024 9196 33.05523
2023 9196 33.05523
2022 9196 33.412265
2021 9196 33.77287035
2005 9196 39.11636105858664
2004 9196 39.42959579152604
2003 9196 39.745179784962485
2002 9196 40.0631306583497
2001 9196 40.38346616328733
2000 9196 42.02154384293608
1999 9196 42.02154384293608
1998 9196 42.02154384293608
1997 9196 42.02154384293608
2024 6454 37.494306250000005
2023 6454 37.8957320125
1999 6454 46.95322894974278
1998 6454 48.07765385469215
1997 6454 48.07765385469215
1996 6454 48.07765385469215
1995 6454 48.07765385469215
1994 6454 48.07765385469215
1993 6454 48.07765385469215
您可以使用
LEAD
和 ROW_NUMBER
分析函数:
SELECT year, jobnumber, hourly_rate
FROM (
SELECT year, jobnumber, hourly_rate,
ROW_NUMBER() OVER (PARTITION BY jobnumber ORDER BY hourly_rate DESC) AS rn
FROM (
SELECT year, jobnumber, hourly_rate,
CASE
WHEN LEAD(hourly_rate) OVER (PARTITION BY jobnumber ORDER BY year DESC)
!= hourly_rate
THEN 1
ELSE 0
END AS has_changed
FROM table_name
)
WHERE has_changed = 1
)
WHERE rn = 1;
对于样本数据:
CREATE TABLE table_name (Year, Jobnumber, hourly_rate) AS
SELECT 2024, 1126, 35.582755 FROM DUAL UNION ALL
SELECT 2023, 1126, 35.96506525 FROM DUAL UNION ALL
SELECT 2022, 1126, 36.3511986025 FROM DUAL UNION ALL
SELECT 2021, 1126, 36.74119328852501 FROM DUAL UNION ALL
SELECT 2020, 1126, 37.13508792141026 FROM DUAL UNION ALL
SELECT 1999, 1126, 44.59125280927884 FROM DUAL UNION ALL
SELECT 1998, 1126, 44.94554923034844 FROM DUAL UNION ALL
SELECT 1997, 1126, 45.30250287457605 FROM DUAL UNION ALL
SELECT 1996, 1126, 45.662133671135376 FROM DUAL UNION ALL
SELECT 1995, 1126, 45.662133671135376 FROM DUAL UNION ALL
SELECT 1994, 1126, 45.662133671135376 FROM DUAL UNION ALL
SELECT 2024, 9196, 33.05523 FROM DUAL UNION ALL
SELECT 2023, 9196, 33.05523 FROM DUAL UNION ALL
SELECT 2022, 9196, 33.412265 FROM DUAL UNION ALL
SELECT 2021, 9196, 33.77287035 FROM DUAL UNION ALL
SELECT 2005, 9196, 39.11636105858664 FROM DUAL UNION ALL
SELECT 2004, 9196, 39.42959579152604 FROM DUAL UNION ALL
SELECT 2003, 9196, 39.745179784962485 FROM DUAL UNION ALL
SELECT 2002, 9196, 40.0631306583497 FROM DUAL UNION ALL
SELECT 2001, 9196, 40.38346616328733 FROM DUAL UNION ALL
SELECT 2000, 9196, 42.02154384293608 FROM DUAL UNION ALL
SELECT 1999, 9196, 42.02154384293608 FROM DUAL UNION ALL
SELECT 1998, 9196, 42.02154384293608 FROM DUAL UNION ALL
SELECT 1997, 9196, 42.02154384293608 FROM DUAL UNION ALL
SELECT 2024, 6454, 37.494306250000005 FROM DUAL UNION ALL
SELECT 2023, 6454, 37.8957320125 FROM DUAL UNION ALL
SELECT 1999, 6454, 46.95322894974278 FROM DUAL UNION ALL
SELECT 1998, 6454, 48.07765385469215 FROM DUAL UNION ALL
SELECT 1997, 6454, 48.07765385469215 FROM DUAL UNION ALL
SELECT 1996, 6454, 48.07765385469215 FROM DUAL UNION ALL
SELECT 1995, 6454, 48.07765385469215 FROM DUAL UNION ALL
SELECT 1994, 6454, 48.07765385469215 FROM DUAL UNION ALL
SELECT 1993, 6454, 48.07765385469215 FROM DUAL;
输出:
年份 | 职位编号 | HOURLY_RATE |
---|---|---|
1997 | 1126 | 45.30250287457605 |
1999 | 6454 | 46.95322894974278 |
2001 | 9196 | 40.38346616328733 |