--联合结果集union(集合运算符--
select StudentNo as nonono,SUM(StudentResult) from Result where StudentNo=1 group by StudentNo
union
select StudentNo as no,aa='' from Result where StudentNo=2 ---union可以合并多个结果集
--它有两个前提和一个注意:
--1.合并的结果集的列数必须完全一致
--2.合并的多个结果集的对应列的类型需要一致(可以相互转换)
--3.结果集的列名只与第一个结果集有关 --
select top 3 * from Student
union --做了distinct操作
select top 3 * from Student select top 3 * from Student
union all --不做distinct操作,它的合并效率更高,因为没有必须去判断结果记录是否重复
select top 3 * from Student --要求在一个表格中查询出学生的英语最高成绩、最低成绩、平均成绩
select MAX(StudentResult) from Result
select MIN(StudentResult) from Result
select AVG(StudentResult) from Result --
select MAX(StudentResult), MIN(StudentResult), AVG(StudentResult) from Result --
select (select MAX(StudentResult) from Result),(select MIN(StudentResult) from Result),(select AVG(StudentResult) from Result) select MAX(StudentResult) from Result
union
select MIN(StudentResult) from Result
union
select AVG(StudentResult) from Result --查询每一个学员的成绩,同时在最下面显示平均分
union语句中不能添加order by排序,如果加只能加在最后,最后一句的order by只能去选择第一个结果集中的列
select cast(StudentNo as CHAR(2)) as id,StudentResult from Result
union
select '平均分',AVG(StudentResult) from Result order by id Desc select ' '+cast(StudentNo as CHAR(2)) as id,StudentResult from Result
union
select ' 平均分',AVG(StudentResult) from Result

3.UNION的更多相关文章

  1. SQL Server-聚焦UNIOL ALL/UNION查询(二十三)

    前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...

  2. SQL 提示介绍 hash/merge/concat union

    查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...

  3. LINQ to SQL语句(8)之Concat/Union/Intersect/Except

    适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db ...

  4. SQLServer-----Union,Union All的使用方法

    转载: http://blog.csdn.net/kiqinie/article/details/8132485 select a.Name from Material as a union sele ...

  5. 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢

    比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name =  ...

  6. sql union和union all的用法及效率

    UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...

  7. 【oracle】union、union all、intersect、minus 的用法及区别

    一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...

  8. sql with as union all

    WITH RPL (FId,Fname,Forder) AS ( SELECT ment.deptno,ment.deptname,ment.orderno FROM JTERP..fg_depart ...

  9. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  10. LINQ系列:LINQ to SQL Concat/Union

    1. Concat 单列Concat var expr = (from p in context.Products select p.ProductName) .Concat( from c in c ...

随机推荐

  1. C# Winform 脱离 Framework (一)

    Linker是一个命令行工具,它以将我们的.net程序生成可脱离.net framework环境运行的程序 . Linker不支持中文的路径,在程序中也不能有中文的标识符. Linker 有2种部署方 ...

  2. 自执行的匿名函数!function()

    最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...

  3. VBA 每日文件按日期打包存放

    每天省出1小时,换个好心情 1.判断是否存在tempfolder (过渡文件夹) 2.不存在 则在目标目录下新建文件夹tempfolder 在网上看到一种更简单的方法 if dir("e:\ ...

  4. 部分LINUX系统由图形界面启动变更为命令行界面启动的方法

    背景: 图形界面很绚丽,但是现在并不需要图形界面,只需要命令行即可,所以要将图形界面自启动给关闭.    正文:   Centos:     更改文件/etc/inittab的其中一行    id:5 ...

  5. Request.ServerVariables 参数大全

    Request.ServerVariables("Url") 返回服务器地址 Request.ServerVariables("Path_Info") 客户端提 ...

  6. ecshop之transport和jquery冲突之完美解决方案

    众所周知:ecshop的transport.js文件和Jquery是冲突的,两个文件不能同时调用,现给出以下完美解决方案:原因分析:在transport.js文件中,大概 580行到590行之间,这个 ...

  7. Java多线程基础知识(二)

    一. Java线程具有6种状态  NEW 初始状态,线程被创建,但是还没有调用start方法. RUNNABLE 运行状态,java线程将操作系统中的就绪和运行两种状态笼统的称作进行中. BLOCKE ...

  8. Android中加载位图的方法

    Android中加载位图的关键的代码: AssetManager assets =context.getAssets(); //用一个AssetManager 对象来从应用程序包的已编译资源中为工程加 ...

  9. polymorphic-associations 多态关联实例 ruby on rails

    这次做新项目的时候,把图片都放在了一个表里,其他表中不再存图片信息,通过多态关联建立表之间的关系. (1)新建picture表, component表不需要处理 class CreatePicture ...

  10. leetcode 解题报告 Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...