我目前正在超市数据库设计工作,我必须检索折扣后的产品价格。
有两种类型的折扣:直接折扣,数量折扣。产品将有百分比折扣(如:10%折扣)或金额/现金折扣(如:5美元折扣)
目前,我的代码只适用于基于数量的折扣,而不适用于直接折扣(百分比折扣),因为它将返回错误的值。
select id, product_name, unitPrice,Product.discount_cd,
discount_percentange as 'Discount Percentage' ,
discount_amount as 'Discount Amount',
Promotion_Type.quantity,
case when Product.discount_cd NOT LIKE '%DD' AND discount_percentange IS NOT NULL
THEN (unitPrice*ISNULL(discount_percentange,1))*ISNULL(quantity,1)
when (Promotion_Type.discount_cd NOT LIKE '%QB' AND Promotion_Type.discount_percentange IS NOT NULL)
THEN (unitPrice-(unitPrice*discount_percentange))
ELSE (unitPrice*ISNULL(quantity,1))-(unitPrice*ISNULL(discount_percentange,0))-(ISNULL(discount_amount,0))
END AS reduce
from Product
LEFT JOIN Promotion_Type ON Product.discount_cd=Promotion_Type.discount_cd
根据所附图片,产品p001和p005的降价是错误的。我能知道哪里出错了吗?数据库表output database product表的屏幕截图
2条答案
按热度按时间xqkwcwgp1#
我发现你的电脑有很多问题
CASE
声明。首先,我认为你误用了
%
通配符。它表示它所在位置的任意数量的任意字符。例如,'%DD'
只要最后两个字符是'dd'就表示任何内容。添加NOT
把它翻过来。因此,Product.discount_cd NOT LIKE '%DD'
只要Product.discount_cd
不会以'DD'
,这是所有代码。当你加入AND discount_percentage IS NOT NULL
,结果是,任何具有discount_percentage
会和第一个一起回来THEN
声明。这包括p001和p005。第二,我认为你误用了
ISNULL()
功能。没必要用它来做衣服discount_percentage
就像你一样IS NOT NULL
作为标准。另外,如果quantity
指的是产品的数量,我认为你不应该返回1时quantity
为空。假设“dd”代表“直接折扣”,我不明白为什么你的记录有“dd”代码而没有
discount_percentage
. 尽管如此,我认为下面的修改CASE
声明将满足您的需要。p8ekf7hl2#
检查第一个条件,
单位折扣金额应从单价中扣除。