--假设 成绩>100 优

--假设成绩>90 良

select * from TblScore

select 英语成绩=

(case  when tEnglish>90  then '良' when tEnglish>100 then'优'    end),数学成绩=(case when tMath>90  then '良' when tMath>100 then'优'  end) from TblScore

--第二个练习  1  2  3

select * from user5

select 等级=(case when [level]=1 then'骨灰' when [level]=2 then'菜鸟' when [level]=3then '大神' end) from user5

--第三个练习

--6000 5500 4500

select * from MyOrders

select 销售员,销售总金额=SUM(销售数量*销售价格),称号=(

case

    when SUM(销售价格*销售数量)>6000

    then '金牌'

    when SUM(销售价格*销售数量)>5500

    then '银牌'

    when SUM(销售价格*销售数量)>4500

    then '铜牌'

    else

    '通牌'

end

) from MyOrders

group by 销售员



--收入 支出

select * from test

select number,收入=(

case

    when amount>0    

    then amount

    when amount<0

    then 0

end

),支出=(case

when amount<0



then ABS(amount)

when amount>0

then 0



end) from test



--查询全部的英语成绩  并英语的成绩>90 --子查询做

select * from ( select tEnglish from TblScore ) as t where t.tEnglish>90

--查询性别是男 年龄在20岁以上的

select * from( select * from TblStudent where tSGender='男') as t where t.tSAge>20



--1.查询出班级中全部24岁的男生(子查询)

select * from ( select * from TblStudent where tSGender='男') as t where tSAge=24

--2.查询出高一三班和高二二班的全部学生(子查询)

select * from TblStudent where tSClassId in(

 select tClassId from TblClass where tClassName='高一一班' or tClassName='高二二班')

 

 --2.查出黑马一期和黑马二期的全部学生

 use MyItcast

 select * from student

  select * from TblClass

 

  select * from student where TClassId in(select TClassId from TblClass where TClassName='黑马一期' or TClassName='黑马二期' )



--3.查询出的总人数,男同学多少人,数学平均成绩(子查询)

select 总人数=(select COUNT(*)from student) ,男同学多少人=(select COUNT(*) from student where TSGender=1),数学平均成绩=(select AVG(TblScore.TSMath) from TblScore)





--9条到16条的数据



select * from student

select top 8 * from student where TSId not in(select  top 8 TSId from student) --

--16  到 26

select top 8 * from student where TSId not in( select top 15 TSId from student)

select * from student



use nononodeleteImportant



select * from TblStudent



--每页三条  查第五页的



select * from (

select * ,编号=ROW_NUMBER() over(order by tSid) from TblStudent  ) as newTbl where newTbl.编号 between (5-1)*3+1 and 5*3




--每页9条数据 查询13页的

select * from (

select 编号=ROW_NUMBER() over(order by tSId),* from TblStudent) as t where t.编号 between (13-1)*9+1 and 13*9





select tMath,名次= ROW_NUMBER() over(order by tMath)  from TblScore

select tMath,名次=RANK() over(order by tMath) from TblScore --rank同样成绩的排名同样

select * from MyOrders

select 商品名称,行号=ROW_NUMBER() over(partition by 商品名称 order by id) from MyOrders --partition by 分区



--销售员的销售总金额

select * from MyOrders

select 销售员,销售总金额=SUM(销售数量*销售价格) from MyOrders

group by 销售员



--2.统计每一个销售员(订单)的销售金额占总销售金额的百分比。

select * ,销售数量*销售价格,

百分比=销售数量*销售价格*1.0/SUM(销售数量*销售价格) over(partition by 销售员 )*100

from MyOrders





--链接查询



--查询这个学生的时候能不能把这个学生所在的班级的名字也显示出来

select TblStudent.tSName,TblStudent.tSAge,TblStudent.tSGender,TblClass.tClassName from TblStudent

inner join TblClass

on TblStudent.tSClassId=TblClass.tClassId



--查询这个学生在哪个班级,他(她)的考试成绩

select  TblStudent.tSName,TblStudent.tSGender,TblClass.tClassName,TblScore.tEnglish,TblScore.tMath from TblStudent

inner join TblClass

on TblStudent.tSClassId=TblClass.tClassId

inner join TblScore

on TblStudent.tSId=TblScore.tSId

--创建视图

create view vw_Stu_Cla_Sco_newView

as

select  TblStudent.tSName,TblStudent.tSGender,TblClass.tClassName,TblScore.tEnglish,TblScore.tMath from TblStudent

inner join TblClass

on TblStudent.tSClassId=TblClass.tClassId

inner join TblScore

on TblStudent.tSId=TblScore.tSId

--

select * from vw_Stu_Cla_Sco_newView --查询视图

drop view vw_Stu_Cla_Sco_newView --删除视图





--查询年龄超过20岁的学生的姓名、年龄及所在班级



select TblStudent.tSName,TblStudent.tSAge,TblClass.tClassName from TblStudent

inner join

TblClass

on

TblStudent.tSClassId=TblClass.tClassId

inner join

TblScore

on

TblStudent.tSId=TblScore.tSId

where TblStudent.tSAge>20



--

--查询全部学生(參加及未參加考试的都算)及成绩

select * from TblStudent

inner join TblScore

on TblStudent.tSClassId=TblScore.tSId --參加考试的学生



select TblStudent.tSName, TblScore.tMath,TblScore.tEnglish from TblStudent

left join TblScore

on TblStudent.tSClassId=TblScore.tSId --參加考试的学生和没參加考试的学生





select TblStudent.tSName, TblScore.tMath,TblScore.tEnglish from TblStudent

left join TblScore

on TblStudent.tSClassId=TblScore.tSId

where TblScore.tSId is null --没參加考试的学生



--查询全部參加考试的,english分数不为null学生姓名、年龄及成绩



select TblStudent.tSName, TblScore.tMath,TblScore.tEnglish from TblStudent

inner join TblScore

on TblStudent.tSClassId=TblScore.tSId

where TblScore.tEnglish is not null  --參加考试的学生,英语成绩不为null



--练习3:查询全部学生(參加和未參加考试)的学生姓名、年龄、成绩,假设没有參加考试显示缺考,假设小于english&math60分显示不及格

use nononodeleteImportant

select TblStudent.tSName,TblStudent.tSAge,英语成绩=(case

when tEnglish is null

then '缺考'

else

    CONVERT(nvarchar,tEnglish)

 end),数学成绩=(case

    when tMath IS null

    then '缺考'

    else

    CONVERT(nvarchar,tMath)

  end ),是否及格=(case when tEnglish>60 and tMath>60 then '及格'

  else '不及格'  

   end) from TblStudent left join

TblScore on TblStudent.tSId=TblScore.tSId





select * from TblArea

select t.AreaId,t.AreaName,t1.AreaName from TblArea as t inner join TblArea as t1 on  t.AreaPId=t1.AreaId



--声明变量

declare @number int ;

set @number=30;

print @number

select @number

if(@number=30)

begin

    print '好帅'

end

else

begin

    select '真心恶心'

end







declare @avg int =0

set @avg=(select AVG(tMath) from TblScore)

if(@avg>60)

begin

    select top 3 * from TblScore order by tMath desc

end

else

begin

    select top 3 * from TblScore order by tMath asc

end

MsSqlServer 语句的更多相关文章

  1. whdxlib

    1 数据库系统实现 实 验 指 导 书 齐心 彭彬 计算机工程与软件实验中心 2016 年 3 月2目 录实验一.JDBC 应用程序设计(2 学时) ......................... ...

  2. net start sql server (instance)

    如何启动 SQL Server 实例(net 命令) 其他版本   可以使用 Microsoft Windows net 命令启动 Microsoft SQL Server 服务. 启动 SQL Se ...

  3. SQLSERVER 修改数据实例的排序规则

    SQL Server服务器修改排序规则的方法 操作及验证步骤: 1 登录数据库后,查看当前安装数据库默认排序规则的两种方式 方式一.使用SQL Server 2014 Management Studi ...

  4. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  5. 数据库(MSSQLServer,Oracle,DB2,MySql)常见语句以及问题

    创建数据库表 create table person ( FName varchar(), FAge int, FRemark varchar(), primary key(FName) ) 基本sq ...

  6. MSSQLServer基础06(变量,case,选择语句)

    变量 声明:declare @UserName nvarchar(50) 赋值1:set @UserName=N'杨':修改 赋值2:select @UserName=N'牛':修改 输出:print ...

  7. MSSQLServer基础02(SQL语句入门(脚本、命令))

    SQL 全名是结构化查询语言(Structured Query Language),是关系数据库管理系统的标准语言 SQL语句是和DBMS“交谈”专用的语句,不同DBMS都认SQL语法. SQL语句中 ...

  8. 数据库(MSSQLServer,Oracle,DB2,MySql)常见语句以及问题(续1之拼接字符串)

    上一篇文章http://www.cnblogs.com/valiant1882331/p/4056403.html写的太长了,所以就换了一篇,链接上一节继续 字符串的拼接 MySql中可以使用&quo ...

  9. mssqlserver 查询数据库表结构语句

    查询指定表结构的表名.列名.类型.说明.字段长度 select o.name as tableName,c.name as columnName,t.name as columnType,p.valu ...

随机推荐

  1. UVA 1513 - Movie collection(树状数组)

    UVA 1513 - Movie collection option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...

  2. Red Gate系列之五 .NET Reflector 7.6.1.824 Edition .NET程序反编译神器(附插件安装教程2012-10-13更新) 完全破解+使用教程

    原文:Red Gate系列之五 .NET Reflector 7.6.1.824 Edition .NET程序反编译神器(附插件安装教程2012-10-13更新) 完全破解+使用教程 Red Gate ...

  3. 用golang写的 分解x86 intel boot/recovery工具

    源代码地址: https://github.com/sndnvaps/pack-unpack-intel

  4. Unity 捕获IronPython脚本错误

    using System; using System.Collections.Generic; using System.IO; using System.Reflection; using Syst ...

  5. gif动图快速制作方法(附工具)(转)

    现在写博客或是wiki的过程中,会经常引用到图片,特别是客户端经常与页面相关所以截图不可避.但是越来越多的效果仅仅一张图片是无法清楚的描述.并且博客或是wiki也是支持gif图的.gif图的制作方法有 ...

  6. Facebook新框架React Native,一套搞定App开发[转]

    Facebook新框架React Native,一套搞定App开发 本文来自微信公众号“给产品经理讲技术”(pm_teacher),欢迎关注. 做为一名产品经理,你是否遇到过这样的窘境,“帮我把字体调 ...

  7. php+sqlite cms

    1 phpSQLiteCMS 最新版本 phpSQLiteCMS 2.0.4 http://phpsqlitecms.net/ 2 taoCMS  最新版本 [2.5Beta5下载地址] 需要php ...

  8. 【Java基础】System.arraycopy()的使用详解

    由于在Java中System.arraycopy()方法在一维数组和二维数组中的表现不同,所以做了一个测试 public static void main(String[] args) { int[] ...

  9. ym——Android之ListView性能优化

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! Android之ListView性能优化 假设有看过我写过的15k面试题的朋友们一定知 ...

  10. JAVA中的super和this关键字的使用

    一 this关键字 this关键字可以出现在构造方法和实例方法中,不能出现在静态方法中,这是因为静态方法可以用类名来调用,这时可能还没有任何对象诞生. this主要有两种用法: 1 用在构造方法中,调 ...