I have a table like:
| it.name | ReqTime |
|---|---|
| 1 - Hop:IP | 5 |
| 2 - Hop:IP | 1 |
| 3 - Hop:IP | 22 |
| 4 - Hop:IP | 15 |
| 5 - Hop:IP | 30 |
| 6 - Hop:IP | 30 |
select it.name as target_app, hsu.value as req_rate
from items it
inner join hosts hs on hs.hostid = it.hostid
inner join history hsu on hsu.itemid = it.itemid
where it.name like '% - Hop%' and to_timestamp(hsu.clock) BETWEEN NOW() - INTERVAL '1 MINUTES' AND NOW()
order by it.name
How can get a table like this? :
| it.name | it.name (Start from the 2nd hop) | ReqTime |
|---|---|---|
| 1 - Hop:IP | 2 - Hop:IP | 5 |
| 2 - Hop:IP | 3 - Hop:IP | 1 |
| 3 - Hop:IP | 4 - Hop:IP | 22 |
| 4 - Hop:IP | 5 - Hop:IP | 15 |
| 5 - Hop:IP | 6 - Hop:IP | 30 |
CodePudding user response:
You can try to use LEAD window function.
SELECT *
FROM (
select it.name as target_app,
LEAD(it.name) OVER(ORDER BY it.name) the_2nd_hop
hsu.value as req_rate
from items it
inner join hosts hs on hs.hostid = it.hostid
inner join history hsu on hsu.itemid = it.itemid
where it.name like '% - Hop%' and to_timestamp(hsu.clock) BETWEEN NOW() - INTERVAL '1 MINUTES' AND NOW()
) t1
WHERE the_2nd_hop IS NOT NULL
order by target_app
