2

私はこの問題で立ち往生しています。

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

等々

assigned_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
4

0 に答える 0