(SQL SERVER 2005)
我有一个与ItemCode相关的多个产品表。我可以使用下面的查询建立最佳保存(我认为),但我需要包括的是RRP和SellingPrice字段,这些组合提供了最佳的保存。
提前道歉这可能是一个常见问题,但我找不到合适的解决方案。
SELECT ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
FROM ItemCodePricingDetail
WHERE ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y')
AND (RRP > 0) AND ([SellingPrice] > 0)
GROUP BY ItemCode
非常感谢
答案 0 :(得分:1)
select * from ItemCodePricingDetail
JOIN
(
SELECT ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
FROM ItemCodePricingDetail
WHERE ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y')
AND (RRP > 0) AND ([SellingPrice] > 0)
GROUP BY ItemCode
) as t1 on ItemCodePricingDetail.ItemCode=t1.ItemCode
and RRP - [SellingPrice]= t1.BestSaving