当前位置: 移动技术网 > IT编程>数据库>MSSQL > mssql CASE,GROUP BY用法

mssql CASE,GROUP BY用法

2017年12月12日  | 移动技术网IT编程  | 我要评论

徐淑彬,女子产下14斤巨婴,北风那个吹在线观看

复制代码 代码如下:

--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',0,50
union
select '2007-3-1',0,60
union
select '2007-12-1',1,80
union
select '2008-1-1',0,100
union
select '2008-12-1',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),years,120)


select convert(char(4),years,120) as 'year',
isfirstsixmonths=case when isfirstsixmonths=0 then '上半年' else '下半年' end ,
sum(totalcome) as 'sum' from test
group by isfirstsixmonths,convert(char(4),years,120)

--drop database dbtemp

结果如下:
复制代码 代码如下:

2007 上半年 110
2007 下半年 80
2008 上半年 100
2008 下半年 100

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网