Dmytro Mysak
Dmytro Mysak

Reputation: 1252

Select two entry from a joined table (1:m)

I have a device table (dev) and device_date table (dev_data). Relationship 1:M

dev table:

| id  |name   |status |
|-----|-------|-------|
| 1   |a      |111    |
|-----|-------|-------|
| 2   |b      |123    |
|-----|-------|-------|
| ....|.....  |....   |

dev_data table:

|id |dev_id |status  |date                    |
|---|-------|--------|------------------------|
|1  |1      | 123    |2019-04-16T18:53:07.908Z|
|---|-------|--------|------------------------|
|2  |1      | 120    |2019-04-16T18:54:07.908Z|
|---|-------|--------|------------------------|
|3  |1      | 1207   |2019-04-16T18:55:07.908Z|
|---|-------|--------|------------------------|
|4  |2      | 123    |2019-04-16T18:53:08.908Z|
|---|-------|--------|------------------------|
|5  |2      | 121    |2019-04-16T18:54:08.908Z|
|---|-------|--------|------------------------|
|6  |2      | 127    |2019-04-16T18:55:08.908Z|
|...|.......|........|........................|

I need to select all dev and join dev_data, but add only 2 last records (by date)

the final response should look like this one:

status_calc_1 and status_calc_2 is diff between status in dev and dev_data

status_calc_1 => status difference of the last row from dev_data and dev

status_calc_2 => status difference of prelast row from dev_data and dev

|id  |name  |status_calc_1  | status_calc_2 |
|----|------|---------------|---------------|
|1   |a     |1207-111       |120-111        |
|----|------|---------------|---------------|
|2   |b     |127-123        |121-123        |

I tried this one:

select id, "name", status, max(dd.date) as last,
       (select date from device_data p where p.dev_id = device.id and date < dd.date limit 1) as prelast
from device
  inner join device_data dd on device.id = dd.dev_id
group by id, "name", status;

but get an error:

ERROR: subquery uses ungrouped column "device.id" from outer query

and this one:

select id, "name", status, max(dd.date) as last, max(dd2.date) as prelast,
from device
  inner join device_data dd on device.id = dd.dev_id
  inner join device_data dd2 on device.id = dd2.dev_id and dd2.date < dd.date
group by id, "name", status;

I get correct 2 last dev_data, but still, have no idea how to make 2 columns status_calc_1 and status_calc_2

status_calc_1 = last row dev_data.status - dev.status

status_calc_2 = prelast row dev_data.status - dev.status

Upvotes: 2

Views: 65

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269653

You can use conditional aggregation:

select d.id, d.name, d.status,
       max(dd.date) as last,
       max(case when dd.seqnum = 2 then dd.date end) as prelast,
       (max(case when dd.seqnum = 1 then dd.status end) - d.status) as status_calc_1,
       (max(case when dd.seqnum = 2 then dd.status end) - d.status) as status_calc_2
from device d join
     (select dd.*,
             row_number() over (partition by dd.dev_id order by dd.date desc) as seqnum
      from device_data dd
     ) dd
     on d.id = dd.dev_id
where seqnum <= 2
group by d.id, d.name, d.status;

Upvotes: 2

Related Questions