I have two tables where primary key is uniqueidentifier and there are not dates inside of them. One table has info where is stored and has relationship to another table which is tracking status, each status is new row for history purpose. Now the problem is taking last row from second table and status.
Table 1
| Column | Type |
| ------------ | ------------ |
| Id | uniqueidentifier |
| BoxNum | nvarchar(25) |
Table 2
Column | Type |
---|---|
Id | uniqueidentifier |
status | int |
Table1_FK_ID | uniqueidentifier |
Status has the following meaning:
0 for new
10 - approved
20 - rejected
30 - blocked
Each box can be rejected than approved or opposite. So my question is how I can get last inserted record in table2, something like this:
Result
| BoxNum | Status |
| ------------ | ------------ |
| 444444 | 10 |
| 444445 | 20 |
| 444412 | 30 |
2条答案
按热度按时间s8vozzvw1#
If uniqueidentifier is sequential you can also use subquery like that;
But if there is no related data in second table it could be error, then you need to check data existence.
wd2eg0qa2#
I am guessing this is simplified example of your data. Basically, you need a column on which to order the status records in the second table.
If your
uniqueidentifier
is SEQUENTIAL the solution will be:but if it is not you need to add record_id or date_add columns in order to know which record is the last one inserted.