mssql CASE,GROUP BY用法
前端之家收集整理的这篇文章主要介绍了
mssql CASE,GROUP BY用法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="codetitle"><a style="CURSOR: pointer" data="88399" class="copybut" id="copybut88399" onclick="doCopy('code88399')"> 代码如下:
<div class="codebody" id="code88399">
--create database dbTemp
use dbTemp
create table test
(
Pid int identity(1,1) not null primary key,
Years datetime,
IsFirstSixMonths int default(0),--0表示上半年1表示下半年--
TotalCome int
)
insert test
select '2007-1-1',50
union
select '2007-3-1',60
union
select '2007-12-1',1,80
union
select '2008-1-1',100
union
select '2008-12-1',100 select * from test select convert(char(4),Years,120) as 'year',
IsFirstSixMonths=case when IsFirstSixMonths=0 then '上半年' when IsFirstSixMonths=1 then '下半年' END,
sum(totalcome) as 'sum' from test
group by IsFirstSixMonths,convert(char(4),120)
select convert(char(4),
IsFirstSixMonths=case when IsFirstSixMonths=0 then '上半年' ELSE '下半年' END,120) --DROP DATABASE dbtemp