SQL Server How to select the 1st value from duplicate values in multi-table join [duplicate]

hl0ma9xz  于 2023-05-21  发布在  其他
关注(0)|答案(1)|浏览(145)

This question already has answers here:

Get top 1 row of each group (19 answers)
Closed 2 days ago.

I'm trying to write a SQL query to return the first value based on an order by for a date column where duplicates are returned but I am getting one returned row overall (I'm trying to use Select Top 1 , just incorrectly) Here's the query just to get all rows, duplicates included:

SELECT u.userid, Table1.Col1, Table1.Col2, Table1.Col3
FROM Table1
JOIN Table2 ON Table2.PK = Table1.FK
JOIN [User] u ON Table2.UserId = u.UserId
WHERE u.UserId IN (2,4,6,7,10,11,12,8,5)
ORDER BY Table1.Date DESC

Each User is associated with multiple records in Table1 , but I want just the first returned Table1 columns for a User based on the most recent Table1.Date value. I'm assuming I need a subquery, I just don't know to properly construct it and any help is appreciated.

The above query shows current data (top 4 shown below for ease):
| userid | col 1 | col 2 | col 3 |
| ------------ | ------------ | ------------ | ------------ |
| 1 | 3 | 2021 | null |
| 1 | 4 | 2022 | null |
| 2 | 5 | 2021 | null |
| 2 | 6 | 2022 | null |

I would like to return only the 2nd and 4th rows as those are the first column values associated with the users (users 1 and 2 here) and have the greatest date (in col 2, from the ORDER BY at the end of the above query).

zkure5ic

zkure5ic1#

You can use top(1) if you refactor your query to use apply()

Something like the following:

select u.userid, t.*
from [User] u
cross apply (
  select top(1) t1.col1, t1.col2, t1.col3
  from table2 t2
  join table1 t1 on t1.fk = t2.pk
  where t2.userid = u.userid
  order by t1.date desc
)t
where u.UserId in (2,4,6,7,10,11,12,8,5);

相关问题