Skip to content
Advertisement

Is there a way to use IN and it evaluate values left to right in T-SQL?

Simplified example:

I have been searching and cannot find a solid answer on this. I want to use the IN clause to get certain rows, but I want it to work left to right.

In the example above, I would want a row for 2021, then 2015, then 2010 (assuming no seasons were null).

Is there a way to make the IN clause care about order?

EDIT: So by the comments, I can tell my example was no good. I have edited it to hopefully show what the issue was. I am looking for one result, but I need to check the values in the IN clause IN ORDER, and the actual data in my field was not numerical or easily ordered. In the comments it was suggested to use case statements in the order by clause to make this field where it can be ordered. I was unaware you could use case statements in the order by clause. This seems to have resolved my issue.

Advertisement

Answer

The following code demonstrates a couple of ways of filtering and ordering the data. Any can be modified to return just top (1) Points, but the full output is shown for clarity.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement