Se o requisito permanecer para um pivô de 3 preços em uma série, use
ROW_NUMBER()
irá permitir dar estes números 1, 2, 3 e a partir daí é um simples uso de case expressions
com um group by
:por 3 preços mais baixos:
SELECT
a.article_id
, MAX(CASE
WHEN a.price_seq = 1 THEN p.price END) AS price_1
, MAX(CASE
WHEN a.price_seq = 2 THEN p.price END) AS price_2
, MAX(CASE
WHEN a.price_seq = 3 THEN p.price END) AS price_3
FROM (
SELECT
article_id
, price_valid_from
, price_id
, ROW_NUMBER() OVER (PARTITION BY article_id
ORDER BY p.price ASC) AS price_seq
FROM article
LEFT OUTER JOIN price p
ON (a.price_id = p.price_id)
) a
GROUP BY
a.article_id
ORDER BY
a.article_id
para 3 preços mais recentes
SELECT
a.article_id
, MAX(CASE
WHEN a.price_seq = 1 THEN p.price END) AS price_1
, MAX(CASE
WHEN a.price_seq = 2 THEN p.price END) AS price_2
, MAX(CASE
WHEN a.price_seq = 3 THEN p.price END) AS price_3
FROM (
SELECT
article_id
, price_valid_from
, price_id
, ROW_NUMBER() OVER (PARTITION BY article_id
ORDER BY price_valid_from DESC) AS price_seq
FROM article
) a
LEFT OUTER JOIN price p
ON (a.price_id = p.price_id)
GROUP BY
(a.article_id)
ORDER BY
a.article_id
Demo SQLfiddle