0
對不起我只是沒有做橫向連接!PostgreSQL橫向連接到極限組
我有這樣的一個表:
ID | NUMBER | VALUE
-------------------
20 | 12 | 0.7
21 | 12 | 0.8
22 | 13 | 0.8
23 | 13 | 0.7
24 | 13 | 0.9
25 | Null | 0.9
現在我想拿到第2行通過降低VALUE的順序排序每個號碼。
ID | NUMBER | VALUE
-------------------
21 | 12 | 0.8
20 | 12 | 0.7
24 | 13 | 0.9
22 | 13 | 0.8
我試過至今的代碼看起來是這樣的: (實測值:Grouped LIMIT in PostgreSQL: show the first N rows for each group?)
SELECT DISTINCT t_outer.id, t_top.number, t_top.value
FROM table t_outer
JOIN LATERAL (
SELECT * FROM table t_inner
WHERE t_inner.number NOTNULL
AND t_inner.id = t_outer.id
AND t_inner.number = t_outer.number
ORDER BY t_inner.value DESC
LIMIT 2
) t_top ON TRUE
order by t_outer.value DESC;
一切都很好,到目前爲止,它只是好像LIMIT 2不工作。我得到所有NUMBER元素的所有行。
謝謝!這是行得通的。 – BenWhite