简体   繁体   中英

How can I select all rows from a table where the combination of two columns is distinct on each row (SQL Server)?

I have a table like this:

UsersPositions

id          Name          PositionId          UserId          Code
---------------------------------------------------------------
1     |     Produce     |     1        |       1        |     A
2     |     Fruits      |     2        |       2        |     C
3     |     Grocery     |     1        |       3        |     B
4     |     Produce     |     1        |       1        |     A
5     |     Fruits      |     2        |       2        |     C
6     |     Dairy       |     4        |       8        |     F

How can I select all results from this table, but remove duplicate entries in which the combination of PositionId and UserId is the same?

Where essentially, the result of the select statement would be this:

id          Name          PositionId          UserId          Code
---------------------------------------------------------------
1     |     Produce     |     1        |       1        |     A
2     |     Fruits      |     2        |       2        |     C
3     |     Grocery     |     1        |       3        |     B
6     |     Dairy       |     4        |       8        |     F

I want to filter using DISTINCT or GROUP BY on PositionId and UserId. I know I can easily get a list of unique values using:

SELECT UserId, PositionId 
FROM UsersPositions
GROUP BY UserId, PositionId

But I also want to grab the other columns. From what I've looked on SO, it seems like I would want to make this a subquery and join it into another query. But how can I do that?

I saw something like this: Finding duplicate values in a SQL table , but it doesn't account for other columns. This post Selecting Distinct combinations. has a few answers with INNER JOIN, but it doesn't work correctly when I try that.

I thought this would be a quick google search for me, but I can't seem to find anything that works on SQL Server. What's the best way to handle this kind of query?

This looks like a perfect case to use a CTE with window funtions .

;with cte
as (select id, [name], positionID, UserID, Code, row_number() over(partition by positionID, UserID order by id) rn
from tbl)
select * from cte where rn=1

From your sample data, all four columns other than id are the same. If that is the case, you can use aggregation:

select max(id), Name, PositionId, UserId, Code
from t
group by Name, PositionId, UserId, Code;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM