Hope someone can help, you guys usually have the answers :-)I have two tables and I'm using DENSE_RANK to only retrieve the most recent record from one of them. I need to bring in a value from a second table, anyone know how?Here's some sample code:DECLARE @paytype AS TABLE (PayNo INT, PayrollNo INT, Pay DECIMAL (25, 10))INSERT INTO @paytypeVALUES (1, 10000, 1200.00), (2, 10000, 1300.00), (3, 10000, 1250.00), (4, 10100, 2000.00), (5, 10100, 2200.00), (6, 10100, 2550.00), (7, 10200, 1000.00), (8, 10200, 1100.00), (9, 10200, 1200.00) DECLARE @unit AS TABLE (PayrollNo INT, Unit INT)INSERT INTO @unitVALUES (10000, 475), (10100, 566), (10200, 575)SELECT PayRollNo, Pay FROM(SELECT PayNo, PayrollNo, Pay,DENSE_RANK() OVER (PARTITION BY PayrollNo ORDER BY PayNo DESC, Pay DESC) RANKFROM @paytype) XWHERE RANK = 1
I want to bring in the unit so my output would look like:PayRollNo Pay Unit10000 1250.0000000000 47510100 2550.0000000000 56610200 1200.0000000000 575
Any pointers appreciated.