--排序
select Row_Number() over(order by a.UserName) as Num
--区分性别
case Sex when 0 then '男' else '女' end SexName

Sqlserver中tinyint, smallint, int, bigint的区别

bigint:从-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型数据,存储大小为 8 个字节。一个字节就是8位,那么bigint就有64位

int:从-2^31(-2,147,483,648)到2^31-1(2,147,483,647)的整型数据,存储大小为 4 个字节。int类型,最大可以存储32位的数据

smallint:从-2^15(-32,768)到2^15-1(32,767)的整数数据,存储大小为 2 个字节。smallint就是有16位

tinyint:从0到255的整数数据,存储大小为 1 字节。tinyint就有8位。

case when以及时间格式化

select a.ApplyID,convert(nvarchar(20),a.TripTime,101) as TripTime,a.[Target],a.ApplyerNum,a.ApplyerName,
case a.CheckState when 0 then '待审核'
when 1 then '审核通过'
when 2 then '退回'
else '' end as CheckState,
case a.AssignState when 0 then '待分派'
when 1 then '已分派'
else '-' end as AssignState,
case a.DriverState when 0 then '退回'
when 1 then '已确认'
else '-' end as DriverState
from cmApply a

将选出来的结果集插入到临时表(该方式会自动创建临时表#tabSubject)

 select * into #tabSubject from   --a.SubjectID,a.Question,a.CorrectAnswer,a.Explain,a.SubjectTypeID,a.CreateID,a.CreateDate,a.SubjectScore,a.ScoreSort
(--题目表(传入参数 HistPaperID,subjecttitleid)
select a.SubjectID,Question,Answer as CorrectAnswer,Explain,TypeID as SubjectTypeID,a.CreateID,CreateDate,Score as SubjectScore,Sort as ScoreSort from HistPaperSubject a
left join HistPaperSubjectScore b on a.subjectID=b.subjectID
where a.isdel=0 and a.HistPaperID=60 and b.HistPaperID=60 and b.subjecttitleid=193) t
select * from #tabSubject
drop table #tabSubject

定义临时表并插入数据

 --题目临时表
select * into #tabSubject from --a.SubjectID,a.Question,a.CorrectAnswer,a.Explain,a.SubjectTypeID,a.CreateID,a.CreateDate,a.SubjectScore,a.ScoreSort
(--题目表(传入参数 HistPaperID,subjecttitleid)
select a.SubjectID,Question,Answer as CorrectAnswer,Explain,TypeID as SubjectTypeID,a.CreateID,CreateDate,Score as SubjectScore,Sort as ScoreSort from HistPaperSubject a
left join HistPaperSubjectScore b on a.subjectID=b.subjectID
where a.isdel=0 and a.HistPaperID=60 and b.HistPaperID=60 and b.subjecttitleid=193) t
--学生答题临时表
select * into #tabStudentAnswer from
(select UserPaperID,UserID,SubmitDate,CreateDate,SubjectID,Answer as StudentAnswer,sort as SubjectSort,Score as StudentScore from UserPaper a
left join UserPaperSubject b on a.id=b.userpaperid
where a.HistPaperID=60 and b.HistPaperID=60 and a.[status]=1 and a.isdel=0) t -- and b.subjectid=500 order by UserID,SubjectSort create table #tabResult
(
SubjectID int,
Question nvarchar(MAX),
CorrectAnswer varchar(100),
Explain nvarchar(MAX),
SubjectTypeID int,
CreateID int,
CreateDate datetime,
SubjectScore decimal(3, 1),
ScoreSort int,
DeFenLv float,
[PerCent] varchar(20)
)
declare @SubjectID int,--题目ID
@CorrectAnswer varchar(100),--正确答案
@CorrectNum int,--正确的题目数
@TotalNum int,--总的题目数
@DeFenLv float,--得分率(以浮点数形式表示)
@PerCent varchar(20);--得分率(以百分比形式表示)
while EXISTS(select SubjectID from #tabSubject)--循环题目临时表
begin
select @SubjectID=SubjectID,@CorrectAnswer=CorrectAnswer from #tabSubject;
select @CorrectNum=Count(*) from #tabStudentAnswer where subjectid=@SubjectID and StudentAnswer=@CorrectAnswer--正确的题目数
select @TotalNum=Count(*) from #tabStudentAnswer where subjectid=@SubjectID--总的题目数
select @DeFenLv=convert(float,@CorrectNum)/convert(float,@TotalNum),@PerCent=cast(cast(round(convert(float,@CorrectNum)/convert(float,@TotalNum)*100,0) as decimal(18,0)) as varchar)+'%'
--临时表(题目及其得分率组成)
insert into #tabResult select a.SubjectID,a.Question,a.CorrectAnswer,a.Explain,a.SubjectTypeID,a.CreateID,a.CreateDate,a.SubjectScore,a.ScoreSort,@DeFenLv,@PerCent from #tabSubject a where a.subjectid=@SubjectID
delete from #tabSubject where subjectid=@SubjectID
end
drop table #tabSubject
drop table #tabStudentAnswer
drop table #tabResult
select * from #tabSubject
select * from #tabStudentAnswer where subjectid=500 and Studentanswer='B'
select * from #tabResult order by DeFenLv desc
select Count(1) as CorrectNum from #tabStudentAnswer where subjectid=500 and StudentAnswer='A'--正确的题目数
select Count(1) as TotalNum from #tabStudentAnswer where subjectid=500--总的题目数 --整数相除得到浮点数 并转为百分比
declare @xiaoshudian float;
select @xiaoshudian=convert(float,23)/convert(float,49)
select @xiaoshudian
select convert(float,25)/convert(float,41) as DeFenLv,cast(cast(round(convert(float,25)/convert(float,41)*100,0) as decimal(18,0)) as varchar)+'%' as [PerCent]

sql-case when,row_number的更多相关文章

  1. SQL中的row_number() over()解释

    有一个面试题目, 有一张表,如下: event_type value time : - : : : : : 需要按照event_type排序,返回同一个event_type的,最近时间和次近时间的两个 ...

  2. SQL Server数据库ROW_NUMBER()函数使用详解

    SQL Server数据库ROW_NUMBER()函数使用详解 摘自:http://database.51cto.com/art/201108/283399.htm SQL Server数据库ROW_ ...

  3. SQL case when 的使用总结

    在网上看到一篇关于case when语句的博客,写得很好,我这里是摘录的,还有我的一些体会,原博客地址:SQL Case when 的使用方法. Case具有两种格式.简单Case函数和Case搜索函 ...

  4. SQL Server 中ROW_NUMBER() OVER基本用法

    1.不能排序法 * FROM table1 WHERE id NOT IN ( SELECT TOP 开始的位置 id FROM table1 ) 2.SQL 2000 临时表法 DECLARE @S ...

  5. SQL 序号列ROW_NUMBER,RANK,DENSE_RANK、NTILE

    原文:SQL 序号列ROW_NUMBER,RANK,DENSE_RANK.NTILE SQL 2005新增加相关函数 : ROW_NUMBER,RANK,DENSE_RANK.NTILE 窗口函数 O ...

  6. sql case when 多条件小结

    sql case when 多条件 小结 -- 第一种 格式 : 简单Case函数 : -- 格式说明 -- case 列名 -- when 条件值1 then 选择项1 -- when 条件值2 t ...

  7. SQL CASE语句的使用

    SQL CASE语句的使用 CASE是一个控制流语句,其作用与IF-THEN-ELSE语句非常相似,可根据数据选择值. CASE语句遍历条件并在满足第一个条件时返回值. 因此,一旦条件成立,它将短路, ...

  8. SQL --- Case when 的使用方法

    1. Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' E ...

  9. sql server之ROW_NUMBER() OVER()取每组的第N行数据

    先看个例子: document_id card_holder_id created_date document_type_id 1 1 2015-7-1 1 2 4 2015-7-2 1 3 4 20 ...

  10. sql case 用法总结

    快下班了,抽点时间总结一下sql 的 case 用法. sql 里的case的作用: 用于计算条件列表的表达式,并返回可能的结果之一.sql 的case 类型于编程语言里的 if-esle if-el ...

随机推荐

  1. WINDOWS SERVER 2012标准版密钥

    Windows Server 2012 R2 安装密钥(只适用安装,不支持激活) 标准版 = NB4WH-BBBYV-3MPPC-9RCMV-46XCB MMPXK-NBJDQ-JPM34-WX3FM ...

  2. CRS无法随操作系统自动启动

    1.环境说明 一套RedHat7.5上安装的11.2.0.4 RAC,进行正常的系统维护,重启主机后发现GI集群一直无法启动.   2.问题分析 (1).查看GI集群的OHASD进程的日志,发现操作系 ...

  3. linux系统管理(1)之 内核编译选项查看

    三个方法 proc文件系统 ubunut debain 红帽等 proc文件系统 /proc/config.gz This file shows you the compile-time config ...

  4. 我理解的Future模式

    学而时习之,不亦说乎!                              --<论语> 什么是Future? 考虑一个场景,为了完成某个业务,我需要同时查询三张表的三条独立数据.但 ...

  5. ASP.NET Core中Middleware的使用

    https://www.cnblogs.com/shenba/p/6361311.html   ASP.NET 5中Middleware的基本用法 在ASP.NET 5里面引入了OWIN的概念,大致意 ...

  6. pip和conda安装源更改

    pip和conda安装源更改 python模块安装,使用国内源可以提高下载速度. pip源更改: pip源有好几个,我一直用的清华的pip源,它5分钟同步一次. 临时使用: pip 后加参数 -i h ...

  7. SQL操作Json数据

    转载自: http://blog.csdn.net/yapingxin/article/details/16913275 有小改动.. 支持复杂结构的使用.. 使用Parent_ID来对应Object ...

  8. vue基础知识之vue-resource/axios

    Vue基础知识之vue-resource和axios(三)   vue-resource Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没 ...

  9. Java入门系列-18-抽象类和接口

    抽象类 在第16节继承中,有父类 People People people=new People(); people.sayHi(); 实例化People是没有意义的,因为"人"是 ...

  10. php实现对数组进行编码转换

    1.转换GB2312编码为UTF-8 //更改编码为utf8 protected function array2utf8($array){ $array = array_map(function($v ...