1.派生表(derived table)
select YEAR(orderdate) as orderyear, COUNT(distinct custid) as numcusts
from Sales.Orders
group by YEAR(orderdate);

内联别名
select orderyear, COUNT(distinct custid) 
from (select YEAR(orderdate) as orderyear, custid from Sales.Orders) as D
group by orderyear

外部命名
select orderyear, COUNT(distinct custid) 
from (select YEAR(orderdate), custid from Sales.Orders) as D(orderyear,custid)
group by orderyear;

2.公用表表达式(CTE, common table express)
with C(orderyear, custid) as
(
select YEAR(orderdate), custid
from Sales.Orders
)
select orderyear, COUNT(distinct custid) as numcusts
from C
group by orderyear;

with C1 as
(
select YEAR(orderdate) as orderyear, custid
from Sales.Orders
),
C2 as
(
select orderyear, COUNT(distinct custid) as numcusts
from C1
group by orderyear
)
select orderyear, numcusts
from C2
where numcusts > 70

嵌套CTE
with <CTE_Name>[<target_column_list>]
as
(
<anchor_member>
union all
<recursive_member>
)
<outer_query_against_CTE>

with EmpsCTE as
(
select empid, mgrid, firstname, lastname
from HR.Employees
where empid = 2

union all

select C.empid, C.mgrid, C.firstname, C.lastname
from EmpsCTE as P --这里的引用前一结果集
join HR.Employees as C
on C.mgrid = P.empid
)
select empid, mgrid, firstname, lastname
from EmpsCTE option(maxrecursion 5); -- 这里的引用返回所有结果集

3.视图
use TSQLFundamentals2008
if object_id('Sales.USACusts') is not null
drop view Sales.USACusts;
go
create view Sales.USACusts
as 
select custid, companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, tax
from Sales.Customers
where country = N'USA';
go

在定义表表达式的查询语句中不允许出现order by子句,因此视图定义中也不允许出现。确实须要从视图中返回有序的数据行,应该在使用视图的外部查询中指定一个数据展示用的order by子句。即使使用TOP选项,视图定义中的order by子句只是确保为TOP选项提供逻辑筛选服务,如果查询视图时没有在外部查询中指定order by子句,则仍然无法保证结果集中行的顺序。
alter view Sales.USACusts
as
select top(100) percent 
custid, companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, tax
from Sales.Customers
where country = N'USA'
order by region;
go

select custid, companyname, region from Sales.USACusts; --并不能总能产生按region列进行排序的结果

encryption选项
select object_definition(object_id('Sales.USACusts')); --can get definition of the view

alter view Sales.USACusts with encryption
as
select custid, companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, tax
from Sales.Customers
where country = N'USA';
go

select object_definition(object_id('Sales.USACusts')); -- can not get definition of the view

schemabinding 选项
alter view Sales.USACusts with schemabinding
as
select custid, companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, tax
from Sales.Customers
where country = N'USA';
go

alter table Sales.Customers drop column address; -- error message

check option 选项
alter view Sales.USACusts with check option
as
select custid, companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, tax
from Sales.Customers
where country = N'USA';
go

insert into Sales.USACusts
values (..., 'UK', ...); --error message, not 'USA'

4.内联表值函数(inline TVF, inline table-valued function)--参数化的视图
use TSQLFundamentals2008
if object_id('dbo.fn_GetCustOrders') is not null
drop function dbo.fn_GetCustOrders;
go
create function dbo.fn_GetCustOrders(@cid as int) returns table
as
return 
select orderid, custid, empid, orderdate, requireddate, shippeddate, shipperid, freight, shipname, shipaddress, shipcity, shipregion, shippostalcode, shipcountry
from Sales.Orders
where custid = @cid;
go

select C.orderid, C.custid, D.productid, D.qty
from dbo.fn_GetCustOrders(1) as C
join Sales.OrderDetails as D
on C.orderid = D.orderid;

5.Apply运算符
包括cross apply与outer apply。
cross apply与cross join类似,都是用右表表达式应用到左表中的每一行。与join不同的是,使用cross apply操作符时,对于左表中的每一行,右表表达式可能代表不同的数据行集合。为此,可以在右边使用一个派生表,在派生表的查询中去引用左表列;也可以使用内联表值函数,把左表中的列作为输入参数进行传递。
cross apply在右表为空时不显示左表的行,outer apply则显示左表的行,同时右表同一行的列用null替代。

cross apply:
select C.custid, A.orderid, A.orderdate
from Sales.Customers as C
cross apply 
(
select top(3) orderid, empid, orderdate, requireddate
from Sales.Orders as O
where O.custid = C.custid
order by orderdate desc, orderid desc
) as A

outer apply:
select C.custid, A.orderid, A.orderdate
from Sales.Customers as C
outer apply 
(
select top(3) orderid, empid, orderdate, requireddate
from Sales.Orders as O
where O.custid = C.custid
order by orderdate desc, orderid desc
) as A

T-SQL基础(5) - 表表达式的更多相关文章

  1. 你真的会玩SQL吗?表表达式,排名函数

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  2. Sql — CTE公用表表达式和With用法总结

    CTE(Common Table Expression) 公用表表达式,它是在单个语句的执行范围内定义的临时结果集,只在查询期间有效.它可以自引用,也可在同一查询中多次引用,实现了代码段的重复利用. ...

  3. SQL Server 公用表表达式(CTE)实现递归

    公用表表达式简介: 公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的执行范围内定义的临时结果集.CTE 与派生表类 ...

  4. SQL Server 公用表表达式(CTE)实现递归的方法

    公用表表达式简介: 公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的执行范围内定义的临时结果集.CTE 与派生表类 ...

  5. SQL基础-建表

    一.建表 1.创建表的两种方式 *客户端工具 *SQL语句 2.使用SQL语句创建表 表名和字段名不能使用中文:(一般为字母开头,字母.数字.下划线组成的字符串): CREATE TABLE关键字后跟 ...

  6. SQL基础-连接表

    一.连接表 1.SQL JOIN 忘记在哪保存的某位网友的图,先明白SQL JOIN, 2.关于笛卡尔积 笛卡尔积: 两个集合的乘积 重新建student表和teacher表: student表: C ...

  7. SQL基础-操纵表及插入、查询

    一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...

  8. (二十)sql基础

    sql基础 --单表查询 select * from student; select * from score; --投影查询 select * from student; --条件查询 select ...

  9. 《SQL Server 2012 T-SQL基础》读书笔记 - 5.表表达式

    Chapter 5 Table Expressions 一个表表达式(table expression)是一个命名的查询表达式,代表一个有效的关系表.SQL Server包括4种表表达式:派生表(de ...

随机推荐

  1. Java 螺纹第三版 第一章Thread介绍、 第二章Thread创建和管理学习笔记

    第一章 Thread导论 为何要用Thread ? 非堵塞I/O      I/O多路技术      轮询(polling)      信号 警告(Alarm)和定时器(Timer) 独立的任务(Ta ...

  2. maven使用.02.一些概念

    在上一篇POST中,简要的介绍了一下maven的特点,优势,安装.并建立了一个简单地Hello world工程.这一篇POST中,将主要会介绍一下Maven的一些约定. pom.xml文件 Maven ...

  3. [IDEs]Eclipse设置花括号样式

    用惯Vistual Studio,在使用Eclipse时发现有很多东西还是挺不习惯,第一个就要解决花括号的样式 步骤: 1.Windows->Preferences->Java->C ...

  4. wkhtmtopdf--高分辨率HTML转PDF(一)

    原文:wkhtmtopdf--高分辨率HTML转PDF(一) 一.需求 这次工作中遇到一个需求,要求把网页转换为PDF,穷极了很多的方法,包括尝试了itextsharp来转换,虽然可以实现,但是分辨率 ...

  5. JMX操作ActiveMQ(1)

    我们知道ActiveMQ broker的管理接口是通过JMX方式提供的. 一个简单的访问方式就是通过jconsole,输入 service:jmx:rmi:///jndi/rmi://localhos ...

  6. stripslashes和addslashes的区别

    我们在向mysql写入数据时,比如: mysql_query(”update table set `title`=’kuhanzhu’s blog’”); 那就会出错.同asp时一样,数据库都会对单引 ...

  7. Redis缓存实现单点登录SSO

    .NET基于Redis缓存实现单点登录SSO的解决方案 .NET基于Redis缓存实现单点登录SSO的解决方案   一.基本概念 最近公司的多个业务系统要统一整合使用同一个登录,这就是我们耳熟能详的单 ...

  8. BT渗透工具使用学习笔记

    BT51.信息收集2.扫描工具3.漏洞发现4.社会工程学工具5.运用层攻击MSF6.局域网攻击7.密码破解8.维持访问一.DNS信息收集1.Dnsenum/pentest/enumeration/dn ...

  9. Redis 安装与简单示例(转)

    本文转载自:http://www.cnblogs.com/kissdodog/p/3570984.html Redis的安装 Redis下载地址如下:https://github.com/dmajki ...

  10. ESXI主机打开shell后主机警告处理

    昨天为了配置snmp监控,将几台ESXI 5.5主机的shell 在控制台上从disable状态修改为enable状态后,登陆vcenter后,发现所有的主机都有警告. 处理过程如下: 选中有警告标志 ...