HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Get last two IDs from rows for last two days

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
rowslastidstwogetforfromdays

Problem

I have a table that looks like this:

+------------+------------+--------+----------+--------------+
    | terminalid |    date    | toolid | toolname | workcenterid |
    +------------+------------+--------+----------+--------------+
    |         17 | 2015-12-17 |    132 | RE8      |            7 |
    |         17 | 2015-12-17 |    140 | RE77     |           10 |
    |         17 | 2015-12-17 |    141 | RE0      |            1 |
    |         17 | 2015-12-17 |    153 | RTR      |           13 |
    |         18 | 2015-12-18 |     29 | C47      |           12 |
    |         18 | 2015-12-18 |     53 | C45      |           10 |
    |         18 | 2015-12-18 |     58 | TXR      |            9 |
    |         18 | 2015-12-18 |     61 | BMS      |            6 |
    |         19 | 2015-12-21 |    128 | REV      |            3 |
    |         19 | 2015-12-21 |    135 | REZS     |           12 |
    |         19 | 2015-12-21 |    154 | REP      |            1 |
    |         20 | 2015-12-22 |     21 | RR14     |            9 |
    |         20 | 2015-12-22 |     69 | REX      |            1 |
    |         20 | 2015-12-22 |     71 | REA      |            4 |
    |         20 | 2015-12-22 |     72 | REW      |           11 |
    |         20 | 2015-12-22 |     91 | RER      |            1 |
    +------------+------------+--------+----------+--------------+


I want to get the last 2 terminalids from the rows where the date is today or yesterday.

For example, if today was 2015-12-18, the query should return me this:

```
+------------+------------+--------+----------+--------------+
| terminalid | date | toolid | toolname | workcenterid |
+------------+------------+--------+----------+--------------+
| 17 | 2015-12-17 | 132 | RE8 | 7 |
| 17 | 2015-12-17 | 140 | RE77 | 10 |
| 17 | 2015-12-17 | 141 | RE0 | 1 |
| 17 | 2015-12-17 | 153 | RTR |

Solution

You can use the rank function to get the last two terminalid's:

select terminalid, date, toolid, toolname, workcenterid
from (
    select terminalid, date, toolid, toolname, workcenterid
         , dense_rank() over ( order by terminalid desc ) as rnk
    from T
    where date <= now() -- this will be dependent of your DBMS
) as X
where rnk <= 2;


Note that date is a reserved word so it is wise to name the column otherwise. Beside that it is a date of some kind (hard to tell from the question), say transaction_date in lack of a better understanding of your domain.

Code Snippets

select terminalid, date, toolid, toolname, workcenterid
from (
    select terminalid, date, toolid, toolname, workcenterid
         , dense_rank() over ( order by terminalid desc ) as rnk
    from T
    where date <= now() -- this will be dependent of your DBMS
) as X
where rnk <= 2;

Context

StackExchange Database Administrators Q#139610, answer score: 2

Revisions (0)

No revisions yet.