标题:SQL case 语句怎么用
只看楼主
_eagle_ice
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-3-15
 问题点数:0 回复次数:2 
SQL case 语句怎么用
最好能给个例子  谢谢
搜索更多相关主题的帖子: SQL case 语句 例子 
2008-04-25 13:40
lff642
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-11-28
得分:0 
试一下
create table tb (UNO int,XM varchar(20),xb char(2))

insert into tb select 1,'tt','男'
union all select  2,'tt2','男'
union all select  3,'tt3','女'
union all select 4,'tt4','女'

select '男生总数' = sum(case when xb='男' then 1 else 0 end),
        '女生总数'= sum(case when xb='女' then 1 else 0 end)
from tb
2008-04-25 14:54
_eagle_ice
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-3-15
得分:0 
case
SELECT   Category =
      CASE type   
         WHEN 'popular_comp' THEN 'Popular Computing'
         WHEN 'mod_cook' THEN 'Modern Cooking'
         WHEN 'business' THEN 'Business'
         WHEN 'psychology' THEN 'Psychology'
         WHEN 'trad_cook' THEN 'Traditional Cooking'
         ELSE 'Not yet categorized'
      END,
   CAST(title AS varchar(25)) AS 'Shortened Title',
   price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
2008-05-15 21:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-211016-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.357360 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved