question

csouchik avatar image
csouchik asked

Finding the MIN of a group of values not used in a previous group

I have been having trouble finding anything on google because I'm not sure how to phrase the search. What I need to do is to select the MIN R from each Id group that hasn't already been selected by a previous group. This is the trimmed down version with an example of a row that has no R value.

I have a table simplified

CREATE TABLE Data(Id int, R int) INSERT INTO Data VALUES(1,1),(2,1),(3,2),(3,3),(4,2),(4,3),(4,4),(5,4),(5,5),(6,5),(6,6),(7,6)

Starting Data

IdR1121323242434454556576

Desired Output

IdR112NULL3243546576

Any help is appreciated.

tsql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
csouchik avatar image
csouchik answered

Sorry, put the data in tables, but looks like it didn't format correctly.

Starting Data Result

Id R Id R

1 1 1 1

2 1 2 NULL

3 2 3 2

3 3 4 3

4 2 5 4

4 3 6 5

4 4 7 6

5 4

5 5

6 5

6 6

7 6

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.