在sqlserver中如何使用CTE解决复杂查询问题

最近,同事需要从数个表中查询用户的业务和报告数据,写了一个sql语句,查询比较慢:

rush:sql;"> Select S.Name,S.AccountantCode,( Select COUNT(*) from ( Select distinct BusinessBackupId from Biz_BusinessBackupCustomer where Id in ( Select BusinessBackupCustomerId from Rpt_RegistForm where ( SignatureCPA1Id=S.Id or SignatureCPA2Id=S.Id ) and DocStatus=30 ) ) T ) as 'BNum',(case when R.Id is null then 0 else 1 end ) as 'Num',R.ReportBackupDate from Base_Staff S left join Rpt_RegistForm R on ( R.SignatureCPA1Id=S.Id or R.SignatureCPA2Id=S.Id ) and R.DocStatus=30 where S.UserType=3

查询需要执行10秒左右,仔细分析,它有2次查询类似的结果集(Base_Staff,Rpt_RegistForm 关联部分),这正是CTE应用的场合。

sqlSERVER 联机丛书,我们来了解下CET的概念:

ms-help://MS.sqlCC.v10/MS.sqlSVR.v10.zh-CHS/s10de_6tsql/html/27cfb819-3e8d-4274-8bbe-cbbe4d9c2e23.htm

指定临时命名的结果集,这些结果集称为公用表表达式 (CTE)。该表达式源自简单查询,并且在单条 SELECT、INSERT、UPDATE、MERGE 或 DELETE 语句的执行范围内定义。该子句也可用在 CREATE VIEW 语句中,作为该语句的 SELECT 定义语句的一部分。公用表表达式可以包括对自身的引用。这种表达式称为递归公用表表达式。

下面看看经过CET改写过的查询

rush:sql;"> With CTE as ( select --s.Id as S_ID,s.Name,s.AccountantCode,r.BusinessBackupCustomerId --,r.Id as R_ID,r.SignatureCPA1Id,r.SignatureCPA2Id from Base_Staff S left join Rpt_RegistForm R on ( R.SignatureCPA1Id=S.Id or R.SignatureCPA2Id=S.Id ) and r.DocStatus=30 where s.UserType=3 ) select t0.*,( Select COUNT(*) from ( Select distinct BusinessBackupId from Biz_BusinessBackupCustomer b inner join CTE on b.Id =CTE.BusinessBackupCustomerId where t0.AccountantCode=CTE.AccountantCode ) t1 ) as '约定书数' from ( select Name,AccountantCode,COUNT( BusinessBackupCustomerId) as '报告数' from CTE group by Name,AccountantCode ) t0

执行此查询,只需要5秒钟时间,比原来的查询提高了一倍。

函数,它统计一个列,如果该列在某行的值为NULL,将不会统计该行,这正符合需求。

另外,CTE还可以做递归处理,详细见上面的联机丛书URL的内容说明。

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...