mssql CASE,GROUP BY用法

2016-02-19 10:17 11 1 收藏

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐mssql CASE,GROUP BY用法,无聊中的都看过来。

【 tulaoshi.com - 编程语言 】

代码如下:

--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

来源:http://www.tulaoshi.com/n/20160219/1594056.html

延伸阅读
sql2000安全很重要 将有安全问题的SQL过程删除.比较全面.一切为了安全! 删除了调用shell,注册表,COM组件的破坏权限 use master EXEC sp_dropextendedproc 'xp_cmdshell' EXEC sp_dropextendedproc 'Sp_OACreate' EXEC sp_dropextendedproc 'Sp_OADestroy' EXEC sp_dropextendedproc 'Sp_OAGetErrorInfo&...
表的索引与附在一本书后面的索引非常相似。它可以极大地提高查询的速度。对一个较大的表来说,通过加索引,一个通常要花费几个小时来完成的查询只要几分钟就可以完成。因此没有理由对需要频繁查询的表增加索引。 注意: 当你的内存容量或硬盘空间不足时,也许你不想给一个表增加索引。对于包含索引的数据库,SQL Sever需要一个可观的额外空间...
--字段添加说明 EXEC sp_addextendedproperty 'MS_Description', '要添加的说明', 'user', dbo, 'table', 表名, 'column', 列名 --删除字段说明 EXEC sp_dropextendedproperty 'MS_Description', 'user', dbo, 'table', 表名, 'column', 字段名 --查看字段说明 代码如下: SELECT [Table Name] = i_s.TABLE_NAME, [Column Name] = i_s.C...
标签: SQLServer
SQL简介 MSSQL是由微软开发的支持SQL的数据库,主要运行于微软的Windows操作系统上,从MSSQL7.0开始盛行于桌面操作系统,目前使用最广泛的版本是MSSQL 2000 Server,现在微软正在加紧开始它的下一个MSSQL版本MSSQL 2005 Server,也叫Microsoft SQL Server Yukon,它的第一个测试版已经发放。 MSSQL 2000的安装 安装过程十分...

经验教程

476

收藏

35
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部