简体   繁体   English

更正此 CASE 表达式?

[英]Correct this CASE expression?

I am receiving a syntax error for the following code.What am I doing wrong?我收到以下代码的语法错误。我做错了什么?

These are the series of questions.. I am on #4 (all others were executed correctly).这些是一系列问题.. 我在#4(所有其他问题都正确执行)。

  1. The marketing department wants to know if the Road-650 bicycle has more than 10 styles currently available.市场部想知道 Road-650 自行车目前是否有 10 辆以上 styles 可用。 Using the Product table, create a statement using IF THEN logic to trigger on whether there are more or less than 10 of Road-650 bicycle units available.使用 Product 表,使用 IF THEN 逻辑创建一个语句来触发可用的 Road-650 自行车数量是多于还是少于 10 辆。 Submit the statement and the result of the query.提交语句和查询结果。

  2. The marketing department has decided that it will only feature black colored Road-650 bicycles in the sales promotion.营销部门已决定在促销中仅使用黑色 Road-650 自行车。 However, the marketing department may want to modify the sale at a later date.但是,营销部门可能希望在以后修改销售。 Use a DECLARE and SET statement to pre-set a variable color to "Black."使用 DECLARE 和 SET 语句将可变颜色预设为“黑色”。 Using the variable, run a query to show all the type of bicycles including all fields.使用该变量,运行查询以显示所有类型的自行车,包括所有字段。

  3. Building on the question two activity, add the "Quantity" and "ListPrice."在问题两个活动的基础上,添加“Quantity”和“ListPrice”。 Filter the results to only items in "Finished Goods Storage."将结果过滤为仅“成品存储”中的项目。

  4. Building on the question three activity, the marketing department has decided that any products that currently have more than 100 units in stock will be discounted by 10%.在问题三活动的基础上,市场部决定对目前库存超过 100 件的任何产品提供 10% 的折扣。 All other products will have a 5% discount as part of the promotion.作为促销活动的一部分,所有其他产品将享受 5% 的折扣。 Create a CASE statement that modifies the "ListPrice" into a new field called "SalesPrice."创建一个 CASE 语句,将“ListPrice”修改为一个名为“SalesPrice”的新字段。

DECLARE @color varchar(20)
SET @color = 'black'

SELECT 
    Quantity, ListPrice,
    CASE
       IF Quantity > 100 THEN ListPrice - (ListPrice * .10)
       IF Quantity < 100 THEN ListPrice - (ListPRice * .05)
       ELSE NULL
    END AS SalesPrice
FROM 
    Production.Product
INNER JOIN
    Production.ProductInventory ON Production.ProductInventory.ProductID = Production.Product.ProductID
WHERE 
    color = @color 
    AND LocationID

ERROR:错误:

Incorrect Syntax near the keyword 'IF'关键字“IF”附近的语法不正确
Incorrect Syntax near the keyword 'THEN'关键字“THEN”附近的语法不正确
Incorrect syntax near the keyword 'THEN'关键字“THEN”附近的语法不正确

Change the IF to WHEN将 IF 更改为 WHEN

CASE
WHEN Quantity > 100 THEN ListPrice - (ListPrice * .10)
WHEN Quantity < 100 THEN ListPrice - (ListPRice * .05)
ELSE NULL
END AS SalesPrice

Your case statement should look something like:您的案例陈述应类似于:

CASE
WHEN Quantity > 100 THEN ListPrice - (ListPrice * .10)
ELSE  ListPrice - (ListPRice * .05)
END AS SalesPrice

Keep in mind that your original statement didn't actually have a response for when the Quantity was EQUAL to 100, and would have returned NULL. I don't think that was desired behavior.请记住,当 Quantity 等于 100 时,您的原始语句实际上没有响应,并且会返回 NULL。我认为这不是期望的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM