我有一個定價規則表。我檢索每個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