以下是查询

select *,  "Price Range" =
  CASE
     WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
     WHEN ListPrice < 50 THEN 'Under $50'
     WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
     WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
     ELSE 'Over $1000'
  END
  FROM Production.Product
      ORDER BY ProductNumber

但是存在SQL错误,在FROM之前缺少重要的单词。

如果我想选择所有列,我们使用case的蓝图是什么?

最佳答案

尝试这个

select *,
  CASE
     WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
     WHEN ListPrice < 50 THEN 'Under $50'
     WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
     WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
     ELSE 'Over $1000'
  END AS [Price Range]
  FROM Production.Product
      ORDER BY ProductNumber

关于sql - SQL select case when子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12597328/

10-11 03:32