1、派生表
实质:就是特殊的子查询(将查询结果放在from后面)
含有一张Order表:
看下面的sql语句:
1 select 2 orderid,orderdate,custid 3 from 4 ( 5 select 6 orderid,orderdate,custid,ROW_NUMBER() over(order by orderid) as rownum--列明必须起别名 7 from [Sales.Orders] 8 ) as t--表名必须起别名
需要注意的特殊之处:查询出来的表要起别名,子查询里面的字段名也要起别名
2、CTE(公共表的表达式)
1)语法规范:
1 with use_country--定义的表名 2 as--上面的是 语法: 3 ( 4 select country,companyname,custid 5 from [Sales.Customers] 6 where country='按时打算' 7 ) 8 select * from use_country;--将查询语句 写在 子查询的后面
将查询语句放在最后。
下面看几个例子,具体解析一下:
业务逻辑:还是用上面的那张Order表,要求:查询出 每年的订单数量 大于10的 用户
1》普通的写法:
1 select 2 custid,year(orderdate),COUNT(1) as '订单数量' 3 from [Sales.Orders] 4 group by YEAR(orderdate), custid 5 having COUNT(orderid)>10--having:对 group by 之后的 组函数 进行 筛选使用
2》使用派生表:
1 select 2 custid,orderyear,orderNum 3 from 4 ( 5 select 6 custid,orderyear,COUNT(*) as orderNum 7 from 8 ( 9 select 10 custid, year(orderdate) as orderyear 11 from [Sales.Orders] 12 ) as t1 13 group by orderyear,custid 14 ) as t2 15 where orderNum>10
上面的 查询的层次只有两层,如果层次非常多的话,就会出现,看起来非常费劲了,所以使用CTE的方式更简洁
1 with OrderYear 2 as 3 ( 4 select 5 custid,YEAR(orderdate) as orderyear 6 from [Sales.Orders] 7 ), 8 OrderGroupYear 9 as 10 ( 11 select 12 custid,orderyear,COUNT(orderyear) as ordernum 13 from OrderYear 14 group by orderyear,custid 15 ), 16 OrderNumThanTen 17 as 18 ( 19 select 20 custid,orderyear,ordernum 21 from OrderGroupYear 22 where ordernum>10 23 ) 24 select * from OrderNumThanTen;
2)CTE还可以多张表引用:(相当于c#里面的将重复的代码封装成一个方法)下面举例:
业务逻辑:查询出 每年 客户的数量,以及 前后两年之间客户数量的差量
--首先查询出每年叫客户的数量:
1 select 2 year(orderdate),COUNT(distinct custid)--将 重复的 客户 要去掉 3 from [Sales.Orders] 4 group by YEAR(orderdate)
--然后使用派生表的方式实现:
1 select 2 currtYearOrder.orderYear,currtYearOrder.custCount,prevYearOrder.orderYear,prevYearOrder.custCount,currtYearOrder.custCount-prevYearOrder.custCount 3 from 4 ( 5 select 6 year(orderdate) as orderYear,COUNT(distinct custid) as custCount--将 重复的 客户 要去掉 7 from [Sales.Orders] 8 group by YEAR(orderdate) 9 ) as currtYearOrder 10 left outer join 11 ( 12 select 13 year(orderdate) as orderYear ,COUNT(distinct custid) as custCount--将 重复的 客户 要去掉 14 from [Sales.Orders] 15 group by YEAR(orderdate) 16 ) as prevYearOrder 17 on currtYearOrder.orderYear=prevYearOrder.orderYear+1
--下面使用CTE的方式实现,就不用重复:
1 with OrderYearCust 2 as 3 ( 4 select 5 year(orderdate) as orderYear,COUNT(distinct custid) as custCount--将 重复的 客户 要去掉 6 from [Sales.Orders] 7 group by YEAR(orderdate) 8 ) 9 select 10 curtYearOrder.orderYear,curtYearOrder.custCount,prevYearOrder.orderYear,prevYearOrder.custCount,curtYearOrder.custCount-prevYearOrder.custCount 11 from OrderYearCust as curtYearOrder left outer join OrderYearCust as prevYearOrder on curtYearOrder.orderYear=prevYearOrder.orderYear+1
3)CTE递归查询:(针对 树形节点 进行查询)
表的结果如下:
业务逻辑:需要查询出 mgrid 为 2 下面的 所有的子节点
1 with diguiEmployee 2 as 3 ( 4 --起点:最上层的 查询(只执行一次) 5 select 6 enpid,lastname,firstname,mgrid 7 from [HR.Employees] 8 where mgrid=6 9 10 union all --连接 起点:上层查询 和递归查询 11 12 --递归查询 13 select 14 e.enpid,e.lastname,e.firstname,e.mgrid 15 from [HR.Employees] as e inner join diguiEmployee as d 16 on e.mgrid=d.enpid 17 ) 18 select enpid,lastname,firstname,mgrid from [HR.Employees] where enpid=6 19 union all --将查询的 结果 连接 起来 20 select * from diguiEmployee;