无法使用 AS 设置新列名

Couldn't set new column name using AS

我正在尝试为各自的 IIF 循环创建一个新列,并在此过程中为它们指定一个新的列名,以便它们可以很好地打印在输出中。但是当我尝试执行时,出现语法错误:

Incorrect syntax near the keyword 'AS'.

我尝试从 IIF 语句的每一行中删除 AS ...,我可以成功检索结果 - 只是没有列名。

这是我删除 AS 之前的查询:

SELECT product.productName, price.basePrice, price.promotionValid, discount.finalPrice,
    IIF(((DATEDIFF(dd,startPromotion,GETDATE()) >= 0) AND (DATEDIFF(dd,GETDATE(),endPromotion) >= 0)),
        IIF(price.promotionValid = 0, price.basePrice,
            IIF(discount.promotionType = 0 AND discount.discountMethod = 0, (discount.finalPrice - price.basePrice) AS currencyDiscount,
                IIF(discount.promotionType = 0 AND discount.discountMethod = 1, (price.basePrice / discount.finalPrice)*100 AS percentageDiscount,
                    IIF(discount.promotionType = 1, (discount.finalPrice/discount.bundleQuantity) AS unitPrice, price.basePrice)))), price.basePrice)
FROM product
INNER JOIN price ON price.productName = product.productName
INNER JOIN discount ON price.productName = discount.productName

您不能有条件地将列名分配给您的列。假设您将获得三个结果行。在每一行中,计算出不同的 IIF 结果,因此其含义可能不同。那么应该如何调用包含这些值的列?

您可以包括所有三个额外的列。或者您可以创建一个 "generic" 列来保存您的 "calculation" 结果。

无论哪种方式,您都需要一个额外的列来指示结果类型(基本价格、货币折扣、百分比折扣或单价)。

为了获得一些灵感,我在此处包含了您的查询变体。它包括所有上述策略。请注意,我首先将嵌套的 IIF 转换为 CASE 块。我尽量小心,但我没有测试查询,所以它可能包含错误。

SELECT
  product.productName,
  price.basePrice,
  price.promotionValid,
  discount.finalPrice,
  -- separate calculation result fields:
  discount.finalPrice - price.basePrice AS currencyDiscount,
  (price.basePrice / discount.finalPrice) * 100 AS percentageDiscount,
  discount.finalPrice / discount.bundleQuantity AS unitPrice,
  -- single column containing the conditional calculation result:
  CASE
      WHEN DATEDIFF(dd, startPromotion, GETDATE()) >= 0 AND DATEDIFF(dd, GETDATE(), endPromotion) >= 0 THEN price.basePrice
      WHEN price.promotionValid = 0 THEN price.basePrice
      WHEN discount.promotionType = 0 AND discount.discountMethod = 0 THEN discount.finalPrice - price.basePrice --currencyDiscount
      WHEN discount.promotionType = 0 AND discount.discountMethod = 1 THEN (price.basePrice / discount.finalPrice) * 100 --percentageDiscount
      WHEN discount.promotionType = 1 THEN discount.finalPrice / discount.bundleQuantity --unitPrice
      ELSE price.basePrice
  END AS calculationResult,
  -- column indicating the effective result type:
  CASE
      WHEN DATEDIFF(dd, startPromotion, GETDATE()) >= 0 AND DATEDIFF(dd, GETDATE(), endPromotion) >= 0 THEN 'base price (promotion not active)'
      WHEN price.promotionValid = 0 THEN 'base price (promotion not valid)'
      WHEN discount.promotionType = 0 AND discount.discountMethod = 0 THEN 'currency discount'
      WHEN discount.promotionType = 0 AND discount.discountMethod = 1 THEN 'percentage discount'
      WHEN discount.promotionType = 1 THEN 'unit price'
      ELSE 'base price (no promotion found)'
  END AS calculationResultType
FROM
  product
  INNER JOIN price ON price.productName = product.productName
  INNER JOIN discount ON price.productName = discount.productName