-
Yitzhak Khabinsky 21,511 Reputation points
2021-02-23T00:26:06.38+00:00 Window function
ROW_NUMBER()
is handy for the task.SQL
-- DDL and sample data population, start DECLARE @tbl TABLE (WONum INT, OpNum DECIMAL(10,2)); INSERT INTO @tbl (WONum, OpNum) VALUES (1396026,40.00 ), (1396026,90.00 ), (1396026,90.00 ), (1396026,120.00), (1427600,130.00), (1456392,120.00), (1456392,130.00), (1458018,230.00), (1458018,280.00), (1460224,40.00 ), (1460224,80.00 ); -- DDL and sample data population, end ;WITH rs AS ( SELECT WONum, OpNum , ROW_NUMBER() OVER (PARTITION BY WONum ORDER BY OpNum DESC) AS seq FROM @tbl ) SELECT WONum, OpNum FROM rs WHERE seq = 1 ORDER BY WONum;
Output
+---------+--------+ | WONum | OpNum | +---------+--------+ | 1396026 | 120.00 | | 1427600 | 130.00 | | 1456392 | 130.00 | | 1458018 | 280.00 | | 1460224 | 80.00 | +---------+--------+
Hi @Alvord, Timothy ,
You can also try the following. Thanks!
SELECT WONum, MAX(OpNum) as OpNum
FROM tblHistory
GROUP BY WONum
----------
If the above response is helpful, please accept as answer and up-vote it. Thanks!