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 ...
随机推荐
- [ACM] hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...
- AWS(0) - Amazon Web Services
Computer EC2 – Virtual Servers in the Cloud EC2 Container Service – Run and Manage Docker Containers ...
- 激动啊,终于诞生了,编译了属于俺自己的 JDK
激动啊,终于诞生了,编译了属于俺自己的 JDK 折腾了2天,现在编译过去了 30多分钟了,久违的 java.exe 终于出现在了 bin 目录中,属于俺自己的 JDK 终于诞生了,激动啊
- Java Word Ladder(字梯)
问题: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- 【C语言】写一个函数,实现字符串内单词逆序
//写一个函数,实现字符串内单词逆序 //比如student a am i.逆序后i am a student. #include <stdio.h> #include <strin ...
- mysql导出和导入数据库
出口 在dos计划,切换到mysql按照该文件夹bin下一个.输入以下命令 mysqldump -u root -p nxu_life > nxu_life2.sql 运行完毕后,就能够看到在b ...
- 苹果iOS手机系统诊断功能是后门吗?
7月20日,美国知名苹果iOS手机系统侦破专家扎德尔斯基在2014年世界黑客大会(HOPE/X)用幻灯片讲演揭露了苹果手机存在系统级"后门". 为此,7月23日.苹果公司马上做出回 ...
- wx_sample.php
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin&quo ...
- 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(二)——自定义安装
原文:使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(二)--自定义安装 自定义产品卸载方式 继续从上一次的基础上前进,现在我们已经知道了最简单的bootstr ...
- RVDS 3.1 下载地址及破解方法
RealView Development Suite 3.1 RVDS 3.1下载地址:https://silver.arm.com/download/Development_Tools/RVDS/ ...