I have a table Actions, schema blow:
[Actions]
ActionID
Date
Status <--Nullable, a delta column, only stores value when status changes
Now I want to retrieve the latest record, however it is very likely that Stutus for that record is null, therefore I want to get its last status change(ranked by Date).
Here is an example:
ActionID | Date | Status
------------------------
1 | 04/12| 'Bon'
2 | 04/13| NULL
3 | 04/14| NULL
4 | 04/15| NULL
and my latest record should look like: ActionID: 4, Date: 04/15, Status: 'Bon'
I know it’s possible to do with nested select statements, but in my real table, I have about 10 of these columns, it will drastically affect the performance when a lot of queries like these are made. I wonder if there is a simpler way to do it?
Not sure if I understood your rules, but try this:
You said you have 10 columns.. How it works?
Like scenario A)
..then multiple SELECT TOP 1 subqueries is still best choise I think..
but if it’s like scenario B)
..then you may “reverse” your query like this:
..but be aware you may get empty result if there is no row with StatusA = NOT NULL.