T-SQL基础(5) - 表表达式
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) - 表表达式的更多相关文章
- 你真的会玩SQL吗?表表达式,排名函数
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- Sql — CTE公用表表达式和With用法总结
CTE(Common Table Expression) 公用表表达式,它是在单个语句的执行范围内定义的临时结果集,只在查询期间有效.它可以自引用,也可在同一查询中多次引用,实现了代码段的重复利用. ...
- SQL Server 公用表表达式(CTE)实现递归
公用表表达式简介: 公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的执行范围内定义的临时结果集.CTE 与派生表类 ...
- SQL Server 公用表表达式(CTE)实现递归的方法
公用表表达式简介: 公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的执行范围内定义的临时结果集.CTE 与派生表类 ...
- SQL基础-建表
一.建表 1.创建表的两种方式 *客户端工具 *SQL语句 2.使用SQL语句创建表 表名和字段名不能使用中文:(一般为字母开头,字母.数字.下划线组成的字符串): CREATE TABLE关键字后跟 ...
- SQL基础-连接表
一.连接表 1.SQL JOIN 忘记在哪保存的某位网友的图,先明白SQL JOIN, 2.关于笛卡尔积 笛卡尔积: 两个集合的乘积 重新建student表和teacher表: student表: C ...
- SQL基础-操纵表及插入、查询
一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...
- (二十)sql基础
sql基础 --单表查询 select * from student; select * from score; --投影查询 select * from student; --条件查询 select ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 5.表表达式
Chapter 5 Table Expressions 一个表表达式(table expression)是一个命名的查询表达式,代表一个有效的关系表.SQL Server包括4种表表达式:派生表(de ...
随机推荐
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- OCA读书笔记(10) - 管理UNDO数据
Undo自动管理与手动管理 undo段自动管理SQL> show parameter undo_management 将undo段改为手工管理SQL> alter system set u ...
- android之JSON 进行网络数据交换
什么是JSON JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,同一时候也易于机器解析和生成,很适合于server与client的交互. J ...
- oracle乱码问题
oracle乱码问题通常是因为oracle字符集设置和操作系统字符集设置不一致造成的,这里不得不提到两个操作系统环境变量,LANG和NLS_LANG LANG是针对Linux系统的语言.地区.字符集的 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- Fashion Meets Finance聚会来袭-7月19日 北京
http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NjEzMjMyMQ%3D%3D&appmsgid=10000704&itemidx= ...
- [置顶] JQuery实战总结三 标签页效果图实现
在浏览网站时我们会看到当我们鼠标移到多个选项卡上时,不同的选项卡会出现自己对应的界面的要求,在同一个界面上表达了尽量多的信息.大大额提高了空间的利用率.界面的切换效果也是不错的哦,这次自己可以实现啦. ...
- Hadoop Spark 集群简便安装总结
本人实际安装经验,目的是为以后高速安装.仅供自己參考. 一.Hadoop 1.操作系统一如既往:①setup关掉防火墙.②vi /etc/sysconfig/selinux,改SELINUX=disa ...
- String的Split方法的用法与要注意事项
转自:http://shukuiyan.iteye.com/blog/1058672 之前在http://shukuiyan.iteye.com/blog/507915文中已经叙述过这个问题,但是最近 ...
- Git经常使用命令以及使用方法
一 怎样让单个文件回退到指定的版本号 1. 进入到文件所在文件文件夹,或者能找到文件的路径 查看文件的改动记录 git log MainActivity.java 2. 回退到指定的版本号 ...