SQL Server 基本语法: http://www.w3school.com.cn/sql/sql_intro.asp

练习1:

运行语句:

USE [Test1]
select
FNumber,
(case
when FAmount>0 then FAmount
else 0
end
) as 收入,
(
case
when FAmount<0 then ABS(FAmount)
else 0
end
) as 支出
from Table_1

效果:

练习2:

运行语句:

USE [Test]
select
Name,
(
case Score
when N'胜' then 1
else 0
end
) as 胜,
(
case Score
when N'负' then 1
else 0
end
)as 负
from Table_1

效果:

执行语句:

USE [Test]
select
Name,
Sum(
case Score
when N'胜' then 1
else 0
end
) as 胜,
Sum(
case Score
when N'负' then 1
else 0
end
)as 负
from Table_1
group by Name

效果图:

练习3:

取出通话时间最长的前5个 执行语句:

USE [Test1]
select top 5 * from Table_2
order by DATEDIFF(SECOND,StartDateTime,EndDateTime) Desc

执行结果:

输出所有数据中拨打长途号码(以0开头)的总时长 执行语句:

USE [Test1]
select Sum(DATEDIFF(SECOND,StartDateTime,EndDateTime)) from Table_2
where TelNum like '0%'

执行结果:

输出本月通话总时长最多的前三个呼叫员的编号:执行语句:

USE [Test1]
--select DATEDIFF(Month,convert(datetime,'2015-1-1'),convert(datetime,'2015-2-2')) from Table_2 --select CallerNumber,telNum, DATEDIFF(Month,startDateTime,convert(datetime,'2015-1-2')) from Table_2 --select CallerNumber,telNum, DATEDIFF(Month,startDateTime,GETDATE()) from Table_2 --select * from Table_2
--where DATEDIFF(Month,startDateTime,convert(datetime,'2015-1-15'))=0 select top 3 CallerNumber from Table_2
where DATEDIFF(Month,startDateTime,convert(datetime,'2015-1-15'))=0
group by CallerNumber
order by Sum(DATEDIFF(Month,startDateTime,EndDateTime)) Desc

效果图:

输出一月份拨打次数最多的前三个呼叫员的编号,运行代码:

USE [Test1]
select top 3 CallerNumber,count(*) from Table_2
where DATEDIFF(Month,startDateTime,convert(datetime,'2015-1-15'))=0
group by CallerNumber
order by count(*) Desc

效果图:

输出所有数据的拨号流水,并且在最后一行添加总呼叫时长:

USE [Test1]

select  CallerNumber,TelNum,DATEDIFF(SECOND,startDateTime,EndDatetime) from Table_2

union all

select '汇总',
convert(nchar(20),
SUM(
case when TelNum not like '0%' then DATEDIFF(SECOND,startDateTime,EndDatetime)
else 0
end
)) as 市内通话,
SUM(
case when TelNum like '0%' then DATEDIFF(SECOND,startDateTime,EndDatetime)
else 0
end
) as 长途电话通话
from Table_2

效果图:

SQL Server练习的更多相关文章

  1. 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目

    最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...

  2. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  3. Sql Server系列:分区表操作

    1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...

  4. SQL Server中的高可用性(2)----文件与文件组

        在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...

  5. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

  6. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  7. 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)

    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...

  8. 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  9. SQL Server on Linux 理由浅析

    SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...

  10. SQL Server相关书籍

    SQL Server相关书籍 (排名不分先后) Microsoft SQL Server 企业级平台管理实践 SQL Server 2008数据库技术内幕 SQL Server性能调优实战 SQL S ...

随机推荐

  1. Eclipse+Qt开发环境设置(Linux和Win)

    文章摘要: Windows,Linux平台下安装使用Eclipse + QT4.4.3开发环境 Windows,Linux新建project时的配置(不使用QT预置项目类型,而是手工配置) 使用Ecl ...

  2. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

  3. Internal Server Error500

    开启#LoadModule rewrite_module modules/mod_rewrite.so

  4. webView、scrollView、TableView,为了防止滚动时出现偏移,底部黑框问题等

    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {self.automaticallyAd ...

  5. Hubilder用git插件安装使用

    打开Hbuilder,工具->插件安装(git分布式版本管理插件) 打开https://www.github.com,注册.登录.创建仓库 在Hbuilder中新建项目→然后右键→Team→共享 ...

  6. iOS开发中(null)与<null>的判断

    判断(null): if(m_result==nil) {      NSLog(@"KDA!"); } 判断<null>: if([m_result isEqual: ...

  7. saltstack之(四)远程执行及常用模块

    前几篇文章已经完成了saltstack的安装.认证,从这篇文章开始学习使用saltstack的远程执行. 1.salt远程执行命令详解Usage: salt [options] '<target ...

  8. linux多个python版本下导致no request报错

    解决办法: sudo apt-get install uwsgi uwsgi-core uwsgi-plugin-python sudo apt-get install uwsgi-plugin-py ...

  9. jquery对于table的操作

    $("#datable tr").eq(0).children("td").eq(0).html()   //获得某行某列的值

  10. UCenter uc_user_synlogin同步登陆返回值为空(NULL)的解决办法 及 ucenter原理

    第一种方法最近刚刚接触UCenter,很多问题不是很理解,只是在摸索着.尝试着做,就在刚才有解决了一个问题,虽然不知道解决问题的具体原理,但是还是实现了同步登陆.首先我是在本地测试的,也就是local ...