2

以下是查询

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如果我想选择所有列,我们使用的蓝图是什么?

4

5 回答 5

7

试试这个

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
于 2012-09-26T08:23:14.273 回答
4

把列名放在end之后,你的查询不对,应该是

SELECT p.ProductName, 
  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 Product p

但是您的产品和客户之间是否存在联系

于 2012-09-26T08:15:54.097 回答
2

我认为alias应该在Oracle. 试试这个,

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
于 2012-09-26T08:12:51.300 回答
1

你为什么选择from customer

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 
于 2012-09-26T08:15:09.377 回答
1

它应该是:

 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
于 2012-09-26T08:20:22.693 回答