0

There is a plain sort with order by clause with multiple columns separated by commas. But I am not looking for that. Please read below

The returned default result set is sorted by sequence column. Note sequence 46 and 47 which are in order with different ReqTag and Rank. I am trying to keep the ReqTag column together but sorted by sequence and Rank.

sequence | Reqtag | Rank
-------- | ------ | -------
42       | PD     | 1
43       | PD     | 2
46       | MQ     | 1
46       | SD     | 3
47       | MQ     | 2
47       | SD     | 4
48       | MO     | 1

Now, I want the display to be like below where sequence and rank column are used (desired result set)

sequence | Reqtag | Rank
-------- | ------ | ------
42       | PD     | 1
43       | PD     | 2
46       | MQ     | 1
47       | MQ     | 2
46       | SD     | 3
47       | SD     | 4
48       | MO     | 1

Any ideas are appreciated. Thank you for your time.

2 Answers 2

1

You can use window functions in the sort. I think you want:

order by min(sequence) over (partition by reqtag),
         min(rank) over (partition by reqtag),
         rank
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Gordon. That worked. I am now trying to understand it.
@Guppies . . . You can run the query with those columns added to the select. That should help you understand what the query is doing.
1

If the order of the ReqTag is not important other than that they are grouped together then you should be able to change your sort to be the following:

SELECT sequence, ReqTag, Rank
FROM [Table]
ORDER BY ReqTag, sequence, Rank

1 Comment

I really don't understand how this answer does not meet the OP's requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.