首页 > 代码库 > SQL CASE WHEN ... THEN ... ELSE.. END 实例
SQL CASE WHEN ... THEN ... ELSE.. END 实例
用一个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部分):
SELECTCASE 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_AGROUP BYCASE 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 实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。