当前位置: 首页 > news >正文

SQL 通用表表达式(CTE )

目录

概念:CTE: Common table Expression 

CTE 语法

CTE Demo


概念:CTE: Common table Expression 

通用表表达式(CTE)是SQL中用于简化复杂查询的工具,第一次上线于SQL Server 2005。

CTE提供了一个临时的结果集,可用于SELECT、INSERT、UPDATE、DELETE和MERGE操作。本文详细介绍了CTE的语法、非递归和递归使用方式,并通过实例展示了如何生成行号。非递归CTE用于一次性查询,而递归CTE则能自我引用,直至满足终止条件。CTE在需要临时结果集的场合非常有用。

相比于TempDB而言的优点是减少I/O操作, 不用主动释放。

CTE 语法

CTE语法

-- Demo 1: expression_name 
WITH expression_name [ ( column_name [,...n] ) ] 
AS 
( CTE_query_definition )

select column_name [,...n] from expression_name 


-- Demo 2: expression_name , expression_name2 
WITH expression_name [ ( column_name [,...n] ) ] 
AS 
( CTE_query_definition )
,expression_name2 [ ( column_name [,...n] ) ] 
AS 
( CTE_query_definition )

select column_name [,...n] from expression_name 
union all
select column_name2 [,...n] from expression_name 


-- Demo 3: expression_name , expression_name2 . 此文章中统计结果基于这个脚本
WITH expression_name [ ( column_name [,...n] ) ] 
AS 
( CTE_query_definition )

select column_name [,...n] from expression_name 

; with expression_name2 [ ( column_name [,...n] ) ] 
AS 
( CTE_query_definition )

select column_name2 [,...n] from expression_name 


CTE Demo

DemoDB是主数据库,DemoDB.Archive 是Archive数据库

AbpAuditLogs table scripts

CREATE TABLE [dbo].[AbpAuditLogs](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[TenantId] [int] NULL,
	[UserId] [bigint] NULL,
	[ServiceName] [nvarchar](256) NULL,
	[MethodName] [nvarchar](256) NULL,
	[Parameters] [nvarchar](1024) NULL,
	[ExecutionTime] [datetime] NOT NULL,
	[ExecutionDuration] [int] NOT NULL,
	[ClientIpAddress] [nvarchar](64) NULL,
	[ClientName] [nvarchar](128) NULL,
	[BrowserInfo] [nvarchar](256) NULL,
	[Exception] [nvarchar](2000) NULL,
	[ImpersonatorUserId] [bigint] NULL,
	[ImpersonatorTenantId] [int] NULL,
	[CustomData] [nvarchar](2000) NULL,
 CONSTRAINT [PK_dbo.AbpAuditLogs] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

 用于统计每分钟和每秒钟的并发用户数量。

declare @StartDate datetime=DateAdd(day,-30,getdate());
with tmpT as (
	select UserId,ExecutionTime=FORMAT(ExecutionTime, 'yyyy-MM-dd HH:mm') from AbpAuditLogs where UserId>0 and ExecutionTime>@StartDate
	union all 
	select UserId,ExecutionTime=FORMAT(ExecutionTime, 'yyyy-MM-dd HH:mm') from [DemoDB.Archive].[dbo].[AbpAuditLogs]  where UserId>0 and ExecutionTime>@StartDate
)

select top 10 ExecutionTime,[AVG-User-In-Minutes]=count(distinct UserId) from tmpT group by ExecutionTime order by [AVG-User-In-Minutes] desc

;with tmpTS as (
	select UserId,ExecutionTime=FORMAT(ExecutionTime, 'yyyy-MM-dd HH:mm:ss') from AbpAuditLogs where UserId>0 and ExecutionTime>@StartDate
	union all 
	select UserId,ExecutionTime=FORMAT(ExecutionTime, 'yyyy-MM-dd HH:mm:ss') from [DemoDB.Archive].[dbo].[AbpAuditLogs]  where UserId>0 and ExecutionTime>@StartDate
)

select top 10 ExecutionTime,[AVG-User-In-Second]=count(distinct UserId) from tmpTS group by ExecutionTime order by [AVG-User-In-Second] desc

相关文章:

  • Linux之基本命令和格式
  • RabbitMQ 学习整理2 - 消峰限流
  • C++学习之类和对象基本概念
  • 使用vector构造杨辉三角形
  • 深入理解JavaScript中的同步和异步编程模型及应用场景
  • 【#2】介绍第三方库
  • 全面系统梳理多模态LLM对齐算法
  • Shiro学习(一):Shiro介绍和基本使用
  • vue2拦截器 拦截后端返回的数据,并判断是否需要登录
  • 程序化广告行业(32/89):常见广告位类型深度剖析
  • 【大模型基础_毛玉仁】4.2 参数附加方法
  • spring 核心注解整理
  • Windows 和 Linux 操作系统架构对比以及交叉编译
  • 网络华为HCIA+HCIP 交换机
  • ⭐算法OJ⭐连接所有点的最小费用【最小生成树】(C++实现)Min Cost to Connect All Points
  • 申报视频材料要求!第三批南充西充县非物质文化遗产代表性项目(增补)条件时间和申请程序
  • 原生后台GPS位置限制,降低功耗
  • HarmonyOS NEXT图形渲染体系:重新定义移动端视觉体验
  • 中学数学几百年重大错误:将无穷多各异假R误为R——两数集相等的必要条件
  • ABC391题解
  • 什么能建我的网站呢/seo优化案例
  • cad二次开发网站/营销技巧培训
  • 建外贸网站 东莞/合肥今日头条最新消息
  • 重庆营销网站建设/网站优化网络推广seo
  • 保定市网站制作公司/新闻软文广告
  • 免费的做网站/seo推广优化服务