Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select case when clause

The following is the query

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

But there is SQL error, missing important words before FROM.

What is the blueprint for us to use case if I want to select all the columns ?

like image 228
Raju yourPepe Avatar asked Feb 17 '26 13:02

Raju yourPepe


2 Answers

Try This

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
like image 110
Prasanna Avatar answered Feb 19 '26 03:02

Prasanna


Put the column name after end, your query is not right, should be

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

But is there a link between your Product and Customer

like image 22
codingbiz Avatar answered Feb 19 '26 05:02

codingbiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!