Под другим углом, чтобы посмотреть на него:
DECLARE @A TABLE
(
Price INT,
ID_1 INT,
ID_2 INT,
ID_3 INT
)
INSERT INTO @A
(
Price,
ID_1,
ID_2,
ID_3
)
VALUES
(129.90, 1, 1, 1),
(199.90, 2, 1, 1),
(249.90, 3, 2, 1),
(299.90, 4, 4, 3),
(399.90, 6, 5, 4)
DECLARE @B TABLE
(
Price INT,
PriceID INT
)
INSERT INTO @B
(
Price,
PriceID
)
VALUES
(129.90, 1),
(149.90, 2),
(199.90, 3),
(249.90, 4),
(299.90, 5),
(399.90, 6)
SELECT
a.Price,
SUM(b.NewPrice1),
SUM(b.NewPrice2),
SUM(b.NewPrice3)
FROM @A a
CROSS APPLY
(
SELECT
CASE WHEN PriceID = a.ID_1 THEN Price ELSE NULL END AS NewPrice1,
CASE WHEN PriceID = a.ID_2 THEN Price ELSE NULL END AS NewPrice2,
CASE WHEN PriceID = a.ID_3 THEN Price ELSE NULL END AS NewPrice3
FROM @B
) b
GROUP BY a.Price