探讨SQL Server中Case的不同用法

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

Case 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 Case

首先让我们看一下 Case 的语法。在一般的 SELECT 中,其语法如下:

SELECT <myColumnSpec> = 
Case 
WHEN <A> THEN <somethingA> 
WHEN <B> THEN <somethingB> 
ELSE <somethingE> 
END

在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:

USE pubs 
GO 
SELECT 
    Title, 
    'Price Range' = 
    Case 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END 
FROM titles 
ORDER BY price 
GO

这是 Case 的典型用法,但是使用 Case 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 Case

SELECT 'Number of Titles', Count(*) 
FROM titles 
GROUP BY 
    Case 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END 
GO

你甚至还可以组合这些选项,添加一个 ORDER BY 子句,如下所示:

USE pubs 
GO 
SELECT 
    Case 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END AS Range, 
    Title 
FROM titles 
GROUP BY 
    Case 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END, 
    Title 
ORDER BY 
    Case 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END, 
    Title 
GO

注意,为了在 GROUP BY 块中使用 Case,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 Case 块。

除了选择自定义字段之外,在很多情况下 Case 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集。 <script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
文章标签: sql server null go sql
个人分类: 数据库
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭