這聽起來像的東西,真的應該可解與SQL。但它是不平凡的 - 基本上你需要爲每個產品的所有折扣的乘法骨料。
隨着大量的幫助下Aggregate multiplicate function:
SELECT p.ProductID, p.Product_Name, p.Product_Price * d.Product_Discount AS Discounted_Price
FROM tblProducts p
LEFT JOIN (
SELECT ProductID, EXP(SUM(LOG(1-[Discount]))) AS Product_Discount
FROM tblProd_Disc
GROUP BY ProductID
) d
ON p.ProductID = d.ProductID
但是,如果一個產品有沒有折扣可言,這並不工作。
我試過各種組合Nz()
或IIf()
得到1
爲d.Product_Discount
,但它總是返回#Error
該產品。
您也可以使用UNION查詢解決辦法,將這些產品與未改性的價格。
SELECT p.ProductID, p.Product_Name, p.Product_Price * d.Product_Discount AS Discounted_Price
FROM tblProducts p
INNER JOIN (
SELECT ProductID, EXP(SUM(LOG(1-[Discount]))) AS Product_Discount
FROM tblProd_Disc
GROUP BY ProductID
) d
ON p.ProductID = d.ProductID
UNION
SELECT p.ProductID, p.Product_Name, p.Product_Price
FROM tblProducts p
WHERE NOT EXISTS (
SELECT * FROM tblProd_Disc d WHERE d.ProductID = p.ProductID)
我的樣本數據:
+-----------+--------------+---------------+
| ProductID | Product_Name | Product_Price |
+-----------+--------------+---------------+
| 1 | stuff | 100,00 € |
| 2 | no discount | 100,00 € |
| 3 | one | 100,00 € |
+-----------+--------------+---------------+
+--------+-----------+----------+
| DiscID | ProductID | Discount |
+--------+-----------+----------+
| 1 | 1 | 10,00% |
| 2 | 1 | 15,00% |
| 3 | 3 | 20,00% |
+--------+-----------+----------+
結果:
+-----------+--------------+------------------+
| ProductID | Product_Name | Discounted_Price |
+-----------+--------------+------------------+
| 1 | stuff | 76,5 |
| 2 | no discount | 100 |
| 3 | one | 80 |
+-----------+--------------+------------------+
哇!我不怎麼exp(日誌...的東西的作品,但它的作品!!!!!!謝謝。 – vkat