码迷,mamicode.com
首页 > 数据库 > 详细

SQL CASE WHEN ... THEN ... ELSE.. END 实例

时间:2015-02-03 12:46:17      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

用一个SQL语句完成不同条件的分组(SELECT部分):

select QuoteOrderId,SUM(case when(ApprovalStatus=1)then Amount else 0 end) AS CloseAmount,SUM(case when(ApprovalStatus=2)then Amount else 0 end) AS ApprovalAmount from dbo.PurchaseSettleAccountsDetails a inner join dbo.PurchaseSettleAccounts b on a.PurchaseSettleAccountId=b.ID group by QuoteOrderId

已知数据按照另外一种方式进行分组SELECT部分):

SELECT
CASE WHEN salary <= 500 THEN ‘1‘
WHEN salary > 500 AND salary <= 600  THEN ‘2‘
WHEN salary > 600 AND salary <= 800  THEN ‘3‘
WHEN salary > 800 AND salary <= 1000 THEN ‘4‘
ELSE NULL END salary_class,
COUNT(*)
FROM    Table_A
GROUP BY
CASE WHEN salary <= 500 THEN ‘1‘
WHEN salary > 500 AND salary <= 600  THEN ‘2‘
WHEN salary > 600 AND salary <= 800  THEN ‘3‘
WHEN salary > 800 AND salary <= 1000 THEN ‘4‘
ELSE NULL END;

 

SQL CASE WHEN ... THEN ... ELSE.. END 实例

标签:

原文地址:http://www.cnblogs.com/hongfu/p/4269560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!