SELECT
np.Id,
np.Description,
MIN(Case promotionalCode WHEN 'A' THEN Price ELSE NULL END) AS 'A',
MIN(Case promotionalCode WHEN 'B' THEN Price ELSE NULL END) AS 'B',
MIN(Case promotionalCode WHEN 'C' THEN Price ELSE NULL END) AS 'C'
FROM
Price AS p
INNER JOIN nProduct AS np ON p.nProduct = np.Id
GROUP BY
np.Id,
np.Description
这是一个简单的测试示例:
DECLARE @temp TABLE (
id INT,
description varchar(50),
promotionalCode char(1),
Price smallmoney
)
INSERT INTO @temp
select 1, 'Product 1', 'A', 5
union
SELECT 1, 'Product 1', 'B', 4
union
SELECT 1, 'Product 1', 'C', 2
SELECT
id,
description,
MIN(Case promotionalCode WHEN 'A' THEN Price ELSE NULL END) AS 'A',
MIN(Case promotionalCode WHEN 'B' THEN Price ELSE NULL END) AS 'B',
MIN(Case promotionalCode WHEN 'C' THEN Price ELSE NULL END) AS 'C'
FROM
@temp
GROUP BY
id,
description