1 SELECT productid, productname, categoryid,
2 CASE categoryid
3 WHEN 1 THEN 'Beverages'
4 WHEN 2 THEN 'Condiments'
5 WHEN 3 THEN 'Confections'
6 WHEN 4 THEN 'Dairy Products'
7 WHEN 5 THEN 'Grains/Cereals'
8 WHEN 6 THEN 'Meat/Poultry'
9 WHEN 7 THEN 'Produce'
10 WHEN 8 THEN 'Seafood'
11 ELSE 'Unknown Category'
12 END AS categoryname
13 FROM Production.Products;
输出结果为:
对Sales.OrderValues视图的查询先根据val的逻辑顺序生成3个组,再把组的编号翻译成组的描述信息(Low、Medium、High):
1 SELECT orderid, custid, val, 2 CASE NTILE(3) OVER(ORDER BY val) 3 WHEN 1 THEN 'Low' 4 WHEN 2 THEN 'Medium' 5 WHEN 3 THEN 'High' 6 ELSE 'Unknown' 7 END AS titledesc 8 FROM Sales.OrderValues 9 ORDER BY val;
查询结果如下: