create table ax ( Id int ,上级Id int, 级别 int, 名字 varchar(20) )insert into ax select 2,1,2,'张三' union all select 4,2,3,'李四' union all select 8,4,6,'王五' union all select 9,8,7,'赵六' union all select 12,8,7,'孙七' union all select 15,8,7,'钱八' union all select 33,8,7,'吴九'create table bx( id int, 姓名 varchar(20), 所卖产品 varchar(20) )insert into bx select 12,'孙七','茶碗' union all select 15,'钱八','水缸' union all select 8,'王五','锅' select id,所卖产品, (select 上级Id from ax where Id=A.id) as 上级Id , (select 名字 from ax where Id= (select top 1 上级Id from ax where Id=A.id) ) from bx A
--> 测试数据: [ta] if object_id('[ta]') is not null drop table [ta] go create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4)) insert into [ta] select 2,1,2,'张三' union all select 4,2,3,'李四' union all select 8,4,6,'王五' union all select 9,8,7,'赵六' union all select 12,8,7,'孙七' union all select 15,8,7,'钱八' union all select 33,8,7,'吴九' --> 测试数据: [tb] if object_id('[tb]') is not null drop table [tb] go create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4)) insert into [tb] select 12,'孙七','茶碗' union all select 15,'钱八','水缸' union all select 8,'王五','锅' if object_id('[f_id]') is not null drop function f_id go create function f_id(@id int) returns int as begin declare @n int ;with cte as( select b.id,上级Id ,级别 from ta a,tb b where a.id=b.id union all select a.id,a.上级Id ,a.级别 from ta a,cte b where a.id=b.上级Id ) select @n=id from cte where 级别=3
return @n end go select b.id,所卖产品,分公司经理id=dbo.f_id(b.id),分公司经理=名字 from ta a ,tb b where a.id=dbo.f_id(b.id) id 所卖产品 分公司经理id 分公司经理 ----------- ---- ----------- ----- 12 茶碗 4 李四 15 水缸 4 李四 8 锅 4 李四(3 行受影响)
八楼的应该是没有问题的。 修改八楼的最后一句: select b.id,所卖产品,分公司经理id=a.id,分公司经理=名字 from ta a ,tb b where a.id=dbo.f_id(b.id)楼主看看是否能快点。
有问题,如果添加别的分公司了 ,所有的人只显示一个分公司经理,即使不属于这个公司,但经理还是他 这两表插入下面数据测试下就知道了 if object_id('[ta]') is not null drop table [ta] go create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4)) insert into [ta] select 2,1,2,'张三' union all select 4,2,3,'李四' union all select 8,4,6,'王五' union all select 9,8,7,'赵六' union all select 12,8,7,'孙七' union all select 15,8,7,'钱八' union all select 33,8,7,'吴九' union all --新添加 select 5,2,3,'王1' union all select 53,5,6,'王2' --> 测试数据: [tb] if object_id('[tb]') is not null drop table [tb] go create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4)) insert into [tb] select 12,'孙七','茶碗' union all select 15,'钱八','水缸' union all select 8,'王五','锅' union all --新添加 select 53,'王2','锅2'
if object_id('[ta]') is not null drop table [ta] go create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4)) insert into [ta] select 2,1,2,'张三' union all select 4,2,3,'李四' union all select 8,4,6,'王五' union all select 9,8,7,'赵六' union all select 12,8,7,'孙七' union all select 15,8,7,'钱八' union all select 33,8,7,'吴九' union all --新添加 select 5,2,3,'王1' union all select 53,5,6,'王2' --> 测试数据: [tb] if object_id('[tb]') is not null drop table [tb] go create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4)) insert into [tb] select 12,'孙七','茶碗' union all select 15,'钱八','水缸' union all select 8,'王五','锅' union all --新添加 select 53,'王2','锅2'if object_id('[f_id]') is not null drop function f_id go create function f_id(@id int) returns int as begin declare @n int ;with cte as( select b.id,上级Id ,级别 from ta a,tb b where a.id=b.id AND B.ID=@ID ---上面这个地方漏了一个条件 union all select a.id,a.上级Id ,a.级别 from ta a,cte b where a.id=b.上级Id ) select @n=id from cte where 级别=3
return @n end go select b.id,所卖产品,分公司经理id=dbo.f_id(b.id),分公司经理=名字 from ta a ,tb b where a.id=dbo.f_id(b.id) id 所卖产品 分公司经理id 分公司经理 ----------- ---- ----------- ----- 12 茶碗 4 李四 15 水缸 4 李四 8 锅 4 李四 53 锅2 5 王1(4 行受影响)
---------- --> 测试数据: [ta] if object_id('[ta]') is not null drop table [ta] go create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4)) insert into [ta] select 2,1,2,'张三' union all select 4,2,3,'李四' union all select 8,4,6,'王五' union all select 9,8,7,'赵六' union all select 12,8,7,'孙七' union all select 15,8,7,'钱八' union all select 33,8,7,'吴九' union all --新添加 select 5,2,3,'王1' union all select 53,5,6,'王2' --> 测试数据: [tb] if object_id('[tb]') is not null drop table [tb] go create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4)) insert into [tb] select 12,'孙七','茶碗' union all select 15,'钱八','水缸' union all select 8,'王五','锅' union all --新添加 select 53,'王2','锅2' ---查询语句 BEGIN ;with cte as( select b.id,上级Id ,级别, cast(b.id as varchar)+',' as ss from ta a,tb b where a.id=b.id union all select a.id,a.上级Id ,a.级别, cast(b.ss+ cast(a.id as varchar) as varchar)++',' as ss from ta a,cte b where a.id=b.上级Id )SELECT distinct id,ss=replace((select ','+ss from cte where id=a.id for xml path('')),',,',',') into #temp1 from cte a where a.级别=3
select a.id,a.姓名,a.所卖产品,b.id,c.名字 as 经理名字 from tb a left join #temp1 b on CHARINDEX(','+cast(a.id as varchar)+',',b.ss)>0 left join ta c on b.id=c.id---查询语句END
if object_id('tempdb.dbo.#T1') is not null drop table #T1 create table #T1 (ID int,上级Id int,级别 int,名字 varchar(20)) insert into #T1 select 2, 1, 2, '张三' union all select 4, 2, 3, '李四' union all select 8, 4, 6, '王五' union all select 9, 8, 7, '赵六' union all select 12, 8, 7, '孙七' union all select 15, 8, 7, '钱八' union all select 33, 8, 7, '吴九'if object_id('tempdb.dbo.#T2') is not null drop table #T2 create table #T2 (ID int,名字 varchar(20),所卖产品 varchar(50)) insert into #T2 select 12 ,'孙七' ,'茶碗' union all select 15 ,'钱八', '水缸' union all select 8 ,'王五', '锅'if object_id('tempdb.dbo.#T3') is not null drop table #T3 create table #T3 (ID int,名字 varchar(20),所卖产品 varchar(50)) insert into #T3 select 12 ,'孙七' ,'茶碗' union all select 15 ,'钱八', '水缸' union all select 8 ,'王五', '锅' -- select a.ID 销售员Id ,所卖产品 物品,c.名字 分公司经理, c.Id 分公司经理Id from #T2 a left join #T1 b on a.ID=b.ID left join (select Id ,名字 from #T1) c on c.Id=b.上级Iddrop table #T1 drop table #T2
8 锅 李四 4
12 茶碗 李四 4
这是怎么统计的?
create table ax (
Id int ,上级Id int, 级别 int, 名字 varchar(20)
)insert into ax
select 2,1,2,'张三'
union all
select 4,2,3,'李四'
union all
select 8,4,6,'王五'
union all
select 9,8,7,'赵六'
union all
select 12,8,7,'孙七'
union all
select 15,8,7,'钱八'
union all
select 33,8,7,'吴九'create table bx(
id int, 姓名 varchar(20), 所卖产品 varchar(20) )insert into bx
select 12,'孙七','茶碗'
union all
select 15,'钱八','水缸'
union all
select 8,'王五','锅'
select id,所卖产品,
(select 上级Id from ax where Id=A.id) as 上级Id ,
(select 名字 from ax where Id=
(select top 1 上级Id from ax where Id=A.id) ) from bx A
if object_id('[ta]') is not null drop table [ta]
go
create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4))
insert into [ta]
select 2,1,2,'张三' union all
select 4,2,3,'李四' union all
select 8,4,6,'王五' union all
select 9,8,7,'赵六' union all
select 12,8,7,'孙七' union all
select 15,8,7,'钱八' union all
select 33,8,7,'吴九'
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4))
insert into [tb]
select 12,'孙七','茶碗' union all
select 15,'钱八','水缸' union all
select 8,'王五','锅'
if object_id('[f_id]') is not null drop function f_id
go
create function f_id(@id int)
returns int
as
begin
declare @n int
;with cte as(
select b.id,上级Id ,级别 from ta a,tb b where a.id=b.id
union all
select a.id,a.上级Id ,a.级别 from ta a,cte b where a.id=b.上级Id
)
select @n=id from cte where 级别=3
return @n
end
go select b.id,所卖产品,分公司经理id=dbo.f_id(b.id),分公司经理=名字 from ta a ,tb b where a.id=dbo.f_id(b.id)
id 所卖产品 分公司经理id 分公司经理
----------- ---- ----------- -----
12 茶碗 4 李四
15 水缸 4 李四
8 锅 4 李四(3 行受影响)
修改八楼的最后一句:
select b.id,所卖产品,分公司经理id=a.id,分公司经理=名字 from ta a ,tb b where a.id=dbo.f_id(b.id)楼主看看是否能快点。
这两表插入下面数据测试下就知道了
if object_id('[ta]') is not null drop table [ta]
go
create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4))
insert into [ta]
select 2,1,2,'张三' union all
select 4,2,3,'李四' union all
select 8,4,6,'王五' union all
select 9,8,7,'赵六' union all
select 12,8,7,'孙七' union all
select 15,8,7,'钱八' union all
select 33,8,7,'吴九' union all
--新添加
select 5,2,3,'王1' union all
select 53,5,6,'王2'
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4))
insert into [tb]
select 12,'孙七','茶碗' union all
select 15,'钱八','水缸' union all
select 8,'王五','锅' union all
--新添加
select 53,'王2','锅2'
go
create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4))
insert into [ta]
select 2,1,2,'张三' union all
select 4,2,3,'李四' union all
select 8,4,6,'王五' union all
select 9,8,7,'赵六' union all
select 12,8,7,'孙七' union all
select 15,8,7,'钱八' union all
select 33,8,7,'吴九' union all
--新添加
select 5,2,3,'王1' union all
select 53,5,6,'王2'
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4))
insert into [tb]
select 12,'孙七','茶碗' union all
select 15,'钱八','水缸' union all
select 8,'王五','锅' union all
--新添加
select 53,'王2','锅2'if object_id('[f_id]') is not null drop function f_id
go
create function f_id(@id int)
returns int
as
begin
declare @n int
;with cte as(
select b.id,上级Id ,级别 from ta a,tb b where a.id=b.id AND B.ID=@ID ---上面这个地方漏了一个条件
union all
select a.id,a.上级Id ,a.级别 from ta a,cte b where a.id=b.上级Id
)
select @n=id from cte where 级别=3
return @n
end
go select b.id,所卖产品,分公司经理id=dbo.f_id(b.id),分公司经理=名字 from ta a ,tb b where a.id=dbo.f_id(b.id)
id 所卖产品 分公司经理id 分公司经理
----------- ---- ----------- -----
12 茶碗 4 李四
15 水缸 4 李四
8 锅 4 李四
53 锅2 5 王1(4 行受影响)
--> 测试数据: [ta]
if object_id('[ta]') is not null drop table [ta]
go
create table [ta] (Id int,上级Id int,级别 int,名字 varchar(4))
insert into [ta]
select 2,1,2,'张三' union all
select 4,2,3,'李四' union all
select 8,4,6,'王五' union all
select 9,8,7,'赵六' union all
select 12,8,7,'孙七' union all
select 15,8,7,'钱八' union all
select 33,8,7,'吴九' union all
--新添加
select 5,2,3,'王1' union all
select 53,5,6,'王2'
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (id int,姓名 varchar(4),所卖产品 varchar(4))
insert into [tb]
select 12,'孙七','茶碗' union all
select 15,'钱八','水缸' union all
select 8,'王五','锅' union all
--新添加
select 53,'王2','锅2'
---查询语句 BEGIN
;with cte as(
select b.id,上级Id ,级别, cast(b.id as varchar)+',' as ss from ta a,tb b where a.id=b.id
union all
select a.id,a.上级Id ,a.级别, cast(b.ss+ cast(a.id as varchar) as varchar)++',' as ss from ta a,cte b where a.id=b.上级Id
)SELECT distinct id,ss=replace((select ','+ss from cte where id=a.id for xml path('')),',,',',') into #temp1 from cte a where a.级别=3
select a.id,a.姓名,a.所卖产品,b.id,c.名字 as 经理名字 from tb a
left join #temp1 b on CHARINDEX(','+cast(a.id as varchar)+',',b.ss)>0
left join ta c on b.id=c.id---查询语句END
create table #T1 (ID int,上级Id int,级别 int,名字 varchar(20))
insert into #T1
select 2, 1, 2, '张三' union all
select 4, 2, 3, '李四' union all
select 8, 4, 6, '王五' union all
select 9, 8, 7, '赵六' union all
select 12, 8, 7, '孙七' union all
select 15, 8, 7, '钱八' union all
select 33, 8, 7, '吴九'if object_id('tempdb.dbo.#T2') is not null drop table #T2
create table #T2 (ID int,名字 varchar(20),所卖产品 varchar(50))
insert into #T2
select 12 ,'孙七' ,'茶碗' union all
select 15 ,'钱八', '水缸' union all
select 8 ,'王五', '锅'if object_id('tempdb.dbo.#T3') is not null drop table #T3
create table #T3 (ID int,名字 varchar(20),所卖产品 varchar(50))
insert into #T3
select 12 ,'孙七' ,'茶碗' union all
select 15 ,'钱八', '水缸' union all
select 8 ,'王五', '锅'
--
select a.ID 销售员Id ,所卖产品 物品,c.名字 分公司经理, c.Id 分公司经理Id
from #T2 a
left join #T1 b on a.ID=b.ID
left join (select Id ,名字 from #T1) c on c.Id=b.上级Iddrop table #T1
drop table #T2