递归查询

推广渠道表有ParentID字段,代表上下层级关系。现要统计每个推广员,推广了多少人?

 1 --创建表结构,插入测试数据
 2 USE DBA_Monitor
 3 GO
 4 CREATE TABLE [dbo].[TGYinfo](
 5     [TGYID] [int] IDENTITY(1,1) NOT NULL,
 6     [Name] [varchar](50) NULL,
 7     [ParentID] [int] NULL,
 8     [CreateTime] [datetime] NULL,
 9  CONSTRAINT [PK_tgyTbl] PRIMARY KEY CLUSTERED
10 (
11     [TGYID] ASC
12 )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
13 ) ON [PRIMARY]
14 GO
15 ALTER TABLE [dbo].[TGYinfo] ADD  CONSTRAINT [DF_TGYinfo_CreateTime]  DEFAULT (getdate()) FOR [CreateTime]
16 GO
17 SET IDENTITY_INSERT [dbo].[TGYinfo] ON
18 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (1, N‘推广1‘, 0, CAST(0x0000A4D100182B20 AS DateTime))
19 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (2, N‘推广2‘, 0, CAST(0x0000A4D100182B21 AS DateTime))
20 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (3, N‘推广1_1‘, 1, CAST(0x0000A4D100185C2E AS DateTime))
21 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (4, N‘推广1_2‘, 1, CAST(0x0000A4D100185C2F AS DateTime))
22 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (5, N‘推广1_3‘, 1, CAST(0x0000A4D100185C3D AS DateTime))
23 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (6, N‘推广1_1_1‘, 3, CAST(0x0000A4D10021A746 AS DateTime))
24 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (7, N‘推广1_1_2‘, 3, CAST(0x0000A4D100A1132A AS DateTime))
25 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (8, N‘推广2_1‘, 2, CAST(0x0000A4D100A13AE0 AS DateTime))
26 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (9, N‘推广2_2‘, 2, CAST(0x0000A4D100A1428E AS DateTime))
27 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (10, N‘推广2_1_1‘, 8, CAST(0x0000A4D100A15D3C AS DateTime))
28 INSERT [dbo].[TGYinfo] ([TGYID], [Name], [ParentID], [CreateTime]) VALUES (11, N‘推广2_1_2‘, 8, CAST(0x0000A4D100A161F9 AS DateTime))
29 SET IDENTITY_INSERT [dbo].[TGYinfo] OFF

原始数据如下图所示,需要统计每个节点有多少下层用户


网上很多针对单一节点返回所有下层关系的代码,参考修改运行结果能正常返回。试想如果能将每次的单一节点一并返回出来,问题就基本解决了。或者说用一个字段标识,某N行对应是节点N返回的结果

 1 --单一节点返回所有下层关系的代码
 2 ;with cte as
 3 (select TGYID,Name,ParentID from TGYinfo
 4  WHERE TGYID=1--单一节点
 5 union all
 6 select a.TGYID,a.Name,a.ParentID
 7 from TGYinfo a
 8 inner join cte c
 9 on c.TGYID=a.ParentID
10 )
11 select * from cte


增加OrderID字段,区分数据由哪个节点返回

 1 --生成OrderID字段
 2 ;with cte as
 3 (select TGYID,Name,ParentID,convert(bigint,1000+TGYID) as OrderID--每层用四位编码
 4 from TGYinfo
 5 union all
 6 select a.TGYID,a.Name,a.ParentID,convert(bigint,c.OrderID*10000)+convert(bigint,a.TGYID) OrderID
 7 from TGYinfo a
 8 inner join cte c
 9 on c.TGYID=a.ParentID
10 )
11 select * from cte
12 order by convert(varchar,OrderID)


根据OrderID统计各节点的下层用户数

 1 --最终统计结果
 2 ;with cte as
 3 (select TGYID,Name,ParentID,convert(bigint,1000+TGYID) as OrderID--每层用四位编码
 4 from TGYinfo
 5 union all
 6 select a.TGYID,a.Name,a.ParentID,convert(bigint,c.OrderID*10000)+convert(bigint,a.TGYID) OrderID
 7 from TGYinfo a
 8 inner join cte c
 9 on c.TGYID=a.ParentID
10 )
11 select b.*,a.TGCount from(
12 select left(OrderID,4)%1000 TGYID,count(*) TGCount from cte
13 group by left(OrderID,4)
14 ) a
15 inner join TGYinfo b
16 on a.TGYID=b.TGYID
17 order by a.TGYID

上面这个语句适合表中记录数<10000,如果有多的话,对应代码中的数值要扩大

时间: 2024-10-27 05:59:18

递归查询的相关文章

SQL Server 2005中的CTE递归查询得到一棵树

感觉这个CTE递归查询蛮好用的,先举个例子: [c-sharp] view plain copy print ? use City; go create table Tree ( ID int identity(1,1) primary key not null, Name varchar(20) not null, Parent varchar(20) null ) go insert Tree values('大学',null) insert Tree values('学院','大学') i

Sql Server递归查询(转)

有如下数据表 假如我们要查询ID为003的数据的所有子节点我们可以使用CTE 递归查询完成... [sql] view plaincopyprint? if OBJECT_ID('tb','N') is not null drop table tb; create table tb(id varchar(3) , pid varchar(3) , name varchar(10)); insert into tb values('001' , null , '广东省'); insert into

SQL Server 2005递归查询

? 1 2 3 4 5 6 7 WHIT XXX(列1,列2....) AS (     SELECT 列1,列2... FROM 表WHERE ID=xxxxxx     UNION ALL     SELECT 列1,列2.... FROM 表 WHERE ID = XXX.ParentID ) SELECT     列1,列2.... FROM XXX SQL Server 2005递归查询,布布扣,bubuko.com

SQL 树递归查询

--树递归查询,根遍历子(由外到里递归)WITH MenuTree --AS( SELECT * FROM dbo.HR_SYS_Menu WHERE UpCode = 5 UNION ALL SELECT dbo.HR_SYS_Menu.* FROM MenuTree JOIN dbo.HR_SYS_Menu ON MenuTree.ID = dbo.HR_SYS_Menu.UpCode)SELECT * FROM MenuTreeGO--树递归查询,根遍历子(由外到里递归)WITH Menu

LINQ TO SQL 实现无限递归查询

LINQ TO SQL 实现无限递归查询 现总结一下,希望能给以后再碰到此类问题的朋友一些帮助 --构造测试数据: 只作演示用 CREATE TABLE [dbo].[Tim_LinqTable]( [Id] int PRIMARY KEY IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NOT NULL, [Parent] int NOT NULL, ) GO INSERT INTO [Tim_LinqTable] SELECT 'A',0 UNION

【转】sql递归查询问题

原文链接地址http://www.cnblogs.com/sweting/archive/2009/06/08/1498483.html 在工作中遇到一个问题,是需要sql递归查询的.不懂,于是到csdn上去问,那里的效率真是非常高,我以前也没在上面问过问题. 问题描述: 我有一个表结构如下:id upperid1     23     24     15     3 具体层次不知道,我想用递归sql语句把所有属于某个upperid的数据,包括它的子树,都读出去,请问应该子怎么写?      比

SQL CTE树递归查询

--树递归查询,根遍历子(由外到里递归)WITH MenuTree AS( SELECT * FROM dbo.HR_SYS_Menu WHERE UpCode = 5 UNION ALL SELECT dbo.HR_SYS_Menu.* FROM MenuTree JOIN dbo.HR_SYS_Menu ON MenuTree.ID = dbo.HR_SYS_Menu.UpCode)SELECT * FROM MenuTreeGO --树递归查询,子遍历根(由里到外递归)WITH MenuT

start with connect by prior 递归查询用法

start with connect by prior 递归查询用法 这个子句主要是用于B树结构类型的数据递归查询,给出B树结构类型中的任意一个结点,遍历其最终父结点或者子结点. 先看原始数据: 1 create table a_test 2 ( parentid varchar2(10), 3 subid varchar2(10)); 4 5 insert into a_test values ( '1', '2' ); 6 insert into a_test values ( '1', '

表内父子关系的递归查询

原理:使用公用表表达式的递归查询 传送门:https://msdn.microsoft.com/zh-cn/library/ms186243 问题:指定一个父级,查询出该父级下面的所有节点. 举例说明:存在表 ComponentI为子,PP_ComponentID为父. CREATE PROCEDURE [dbo].[PP1_BOMComponentSelectAll2_SEL] --'01' @ComponentID VARCHAR(20) AS ? BEGIN ? WITH locs(Com

Oracle 递归查询

1 select a.accountcodeid, a.accountcode, a.accountcodename, 2 SYS_CONNECT_BY_PATH(a.accountcodename, '\') as fullname, a.parentcodeid, level 3 from fm_accountcode a 4 where a.accountcodeid >= 10000 5 start with a.parentcodeid is null 6 connect by pri