博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
T-SQL基础(5) - 表表达式
阅读量:5925 次
发布时间:2019-06-19

本文共 4426 字,大约阅读时间需要 14 分钟。

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

 

转载于:https://www.cnblogs.com/thlzhf/p/3407949.html

你可能感兴趣的文章
linux下oracle安装
查看>>
树讲解——紧急集合(lca)
查看>>
路由开发工具记录
查看>>
悠中选优,畅享马尔代夫与塞舌尔之旅
查看>>
Oracle搭建及错误解决办法
查看>>
虚拟机网卡桥接centos系统获取不到IP
查看>>
我的友情链接
查看>>
linux安装maven
查看>>
远程桌面连接不需要密码
查看>>
苹果开发者账号注册-您在注册时提供的地址无效或者不完整
查看>>
UIButton的几种触发方式
查看>>
简体、繁体相互转换
查看>>
java学习记录【day7】
查看>>
java基础---->java中国际化的实现
查看>>
如何在Linux上使用命令行查看硬件信息
查看>>
const char * ptr
查看>>
nginx配置if错误语句时出错一例
查看>>
yum -y install 常用环境、功能模块 解决依赖
查看>>
两台电脑间大量数据拷贝的快捷方法
查看>>
AIX安装SSH
查看>>