我有一个定价规则表。我检索每个ProductTypeID
最大的折扣,这表明产品是哪种类型,使用此查询:加入临时表SQL Server
SELECT MAX(discount) as BiggestDiscount, ProductTypeID FROM dbo.SellingPriceRules
WHERE ProductTypeID is not null
GROUP by ProductTypeID
ORDER BY ProductTypeID
这工作完全,但我需要在这个扩大,并为ProductID
的List找回我最大的折扣。所以我需要找到ProductTypeID
每ProductID
属于和检查我的SellPriceRules
数据库为这个ProductTypeID
的最大折扣。
所以,在我Discounts
表,我有:
ProductID, Margin
在我Products
表我有:
ProductID, ProductTypeID
为了让每一个产品的ProductTypeID,我有:
select * from Discounts m
INNER JOIN Product p on p.ProductID = m.ProductID
WHERE ProductTypeID is not null
我现在正努力将这两个查询结合在一起。我只想获得折扣表中每个产品的最大折扣,并从我的保证金中扣除此折扣。我怎样才能一起加入这两个退休人员?
非常感谢
@Annon你dbo.SellingPriceRules table.http有问题索引扫描操作://sqlfiddle.com/#! 3/4f986/1 –
鉴于您在SellingPriceRules中使用的示例数据,我对您遇到问题并不感到惊讶。 – Anon