我被困在这个问题上。
我有 2 个表格错误和配置文件
profiles table
id name parentid
1 A 0
2 B 1
3 C 1
4 D 2
5 E 2
6 F 3
7 G 5
等等
错误
id assigned_to
1 1
2 3
4 7
等等
assign_to 是配置文件表中 ID 的外键
在单个存储过程中,我需要显示包含给定父级的子级和这些子级子树的错误计数的表
我能够指出当我传递父母姓名时,我可以创建直接孩子的表,但错误的计数仅适用于孩子而不是那个孩子的树
请帮忙
这是我的代码
DELIMITER $$
CREATE DEFINER=`bugs`@`localhost` PROCEDURE `User_hier`(
in loginname varchar(255)
)
BEGIN
declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;
create temporary table ProfileSum(
owner varchar(255),
ownerID smallint unsigned,
P0 smallint unsigned,
P1 smallint unsigned,
P2 smallint unsigned,
P3 smallint unsigned,
P4 smallint unsigned,
P5 smallint unsigned,
SP smallint unsigned,
Total smallint unsigned
)engine = memory;
create temporary table TotalBugByOwner(
owner varchar(255),
ownerID smallint unsigned,
P0 smallint unsigned,
P1 smallint unsigned,
P2 smallint unsigned,
P3 smallint unsigned,
P4 smallint unsigned,
P5 smallint unsigned,
SP smallint unsigned,
Total smallint unsigned,
depth smallint unsigned default 0
)engine = memory;
create temporary table hier(
parent_cat_id smallint unsigned,
cat_id smallint unsigned,
loginemail varchar(255),
depth smallint unsigned default 0
)engine = memory;
insert into ProfileSum
select
p.login_name ,
p.userid ,
count(case when b.priority = 'P0' then b.bug_id else NULL end) AS P0 ,
count(case when b.priority = 'P1' then b.bug_id else NULL end) AS P1 ,
count(case when b.priority = 'P2' then b.bug_id else NULL end) AS P2 ,
count(case when b.priority = 'P3' then b.bug_id else NULL end) AS P3 ,
count(case when b.priority = 'P4' then b.bug_id else NULL end) AS P4 ,
count(case when b.priority = 'P5' then b.bug_id else NULL end) AS P5 ,
count(case when b.priority = 'Set Priority' then b.bug_id else NULL end) AS SetPriority ,
sum(case when b.priority in ('P0' , 'P1' , 'P2' , 'P3' , 'P4' , 'P5' , 'Set Priority') then 1 else NULL end) AS Total
from bugs b
right outer join profiles p on b.assigned_to = p.userid
group by p.login_name ;
insert into hier select MgrID, userid, login_name, v_depth from profiles where login_name = loginname;
create temporary table tmp engine=memory select * from hier;
while not v_done do
if exists( select 1 from profiles p
inner join tmp on p.MgrID = tmp.cat_id and tmp.depth = v_depth) then
insert into hier select p.MgrID, p.userid, p.login_name, v_depth + 1 from profiles p
inner join tmp on p.MgrID = tmp.cat_id and tmp.depth = v_depth;
set v_depth = v_depth + 1;
truncate table tmp;
insert into tmp select * from hier where depth = v_depth;
else
set v_done = 1;
end if;
end while;
insert into TotalBugByOwner
select
PS.owner,
PS.ownerID ,
sum(PS.P0) As P0,
sum(PS.P1) As P1,
sum(PS.P2) As P2,
sum(PS.P3) As P3,
sum(PS.P4) As P4,
sum(PS.P5) As P5,
sum(PS.SP) As SetPriority,
sum(PS.Total) As Total,
h.depth
from ProfileSum PS
JOIN hier h on PS.ownerID = h.cat_id
group by PS.owner;
select * from TotalBugByOwner;
drop temporary table if exists hier;
drop temporary table if exists ProfileSum;
drop temporary table if exists tmp;
drop temporary table if exists totalbugbyowner;
END