Skip to content
Advertisement

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?

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement