I need to write a query that does this:
SELECT TOP 1 FROM a list of tables (Joins, etc) ORDER BY Column X, Column Y, Column Z
If ColumnX is NOT NULL , then at the moment I am reselecting using a slightly different ORDER BY .
So, I am doing the same request twice. If the first has NULL in a specific column, I am returning this row from my procedure. However, if the value is not NULL - I need to make another identical select, except, the order of another column or two.
Now I select it in the temp table for the first time. Then check the value of the column. If everything is in order, return the temp table, otherwise, repeat the selection and return this result set.
More details:
In English, the question I ask in the database is:
Bring back my results for a specific look (by indexed foreign key). I expect about 1000 lines. Order it by appearance date (column, not indexed, nullified), last appearance first. Check "importId". If the import identifier is not NULL for this top 1 row, then we need to run the same query, but this time order by import identifier (last first) and return this row. Or just return the first row from the original query.
Craig source share