查询中的层次结构以获得唯一计数
Hierarchy in a query to get unique count
我有一个 table 可以跟踪当前工作和工作状态。因此,在我的 table 列中,'Current Work' 捕获了产品名称以及在产品上完成的不同任务。
如果作品有子活动,则 'Work Parent Name' 栏会填入 'Current Work',否则如果作品是独立的,则它会留空。因此,无论是在工作级别还是子活动级别,都只需要进行一次计数,因此不能完全用于分层路径。
'Status' 列随工作或子工作的进度更新。
现在,由于数据结构不正确,我发现统计产品的 'In Progress' 状态有点困难。因此,如果该工作下的任何工作或子activity的状态为进行中,我想将工作计算为进行中
所以我正在寻找的输出如下:
我尝试自行加入,但由于子活动相同,我得到了错误的结果。他们有什么方法可以让我从这种类型的数据集中获得我的结果。
好的...我对您的问题有点困惑。如果您正在计数,产品 5 不应该有 2 的进度吗?或者你只是想得到一个 1,如果有任何正在进行中。
试试这个...
select w1.work as work, count(*) countInProgress
from work w1
inner join work w2 on w1.work = w2.parent
where w2.status = 'In Progress'
group by w1.work
或者,如果您还想包括父项的进行中。
select workItem, count(*) countInProgress from
(
select work, @workItem := if(parent is not null,parent,work) workItem
, status
from work
) t
where status = 'In Progress'
group by workItem
无论哪种方式,问题都有点不清楚。产品 2 如何有一个 wip 任务 "In Progress" 但它本身不在进行中?
构建脚本
CREATE TABLE work (work varchar(20), parent varchar(20), status varchar(20));
INSERT INTO work VALUES
('Product 1',null,'In Progress'),
('Service', 'Product 1','Completed'),
('Delivery', 'Product 1', 'In Progress'),
('Transportation', 'Product 1', 'Completed'),
('Product 2',null,'In Progress'),
('Service', 'Product 2',''),
('Delivery', 'Product 2', 'In Progress'),
('Transportation', 'Product 2', ''),
('Product 3',null,'Completed'),
('Product 4',null,'In Progress'),
('Product 5',null,'In Progress'),
('Delivery', 'Product 5', 'In Progress'),
('Transportation', 'Product 5', 'In Progress')
sqlfiddle link 如果你想玩它...
http://sqlfiddle.com/#!9/e0fa02/15
希望这有助于...
评论后编辑:
select w1.work as work
,if(w1.status = 'In Progress' or w2.status = 'In Progress',1,0) countInProgress
from work w1
left join work w2 on w1.work = w2.parent
where w1.parent is null
group by w1.work
试一试...
此外,如果您想查看所有子项目标记为进行中的位置
select w1.work as work
,if(w1.status = 'In Progress' or w2.status = 'In Progress',1,0) countInProgress
,@allInProgress := case when @allInProgress is null and w2.status = 'In Progress' then 1
when @allInProgress is null and (w2.status <> 'In Progress' or w2.status is null) then 0
when w2.status = 'In Progress' then @allInProgress * 1
when w2.status <> 'In Progress' then @allInProgress * 0
end allInProgress
from work w1
left join work w2 on w1.work = w2.parent
where w1.parent is null
group by w1.work
我能够使用两个子查询并通过联合函数组合来实现我的要求。我不确定这是最好的还是唯一的方法,但它给了我想要的并满足了我的要求。
SELECT c.work,
COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) AS inprogress_count
FROM WORK c
INNER JOIN WORK w2 ON c.work = w2.parent
GROUP BY c.work
HAVING COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) <> 0
UNION
SELECT w2.work,
COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) AS inprogress_count
FROM WORK w2
WHERE WORK NOT IN (
SELECT c.work
FROM WORK c
INNER JOIN WORK w2 ON c.work = w2.parent
GROUP BY c.work
HAVING COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) <> 0)
AND parent IS NULL
GROUP BY w2.work;
我有一个 table 可以跟踪当前工作和工作状态。因此,在我的 table 列中,'Current Work' 捕获了产品名称以及在产品上完成的不同任务。
如果作品有子活动,则 'Work Parent Name' 栏会填入 'Current Work',否则如果作品是独立的,则它会留空。因此,无论是在工作级别还是子活动级别,都只需要进行一次计数,因此不能完全用于分层路径。
'Status' 列随工作或子工作的进度更新。
现在,由于数据结构不正确,我发现统计产品的 'In Progress' 状态有点困难。因此,如果该工作下的任何工作或子activity的状态为进行中,我想将工作计算为进行中
所以我正在寻找的输出如下:
我尝试自行加入,但由于子活动相同,我得到了错误的结果。他们有什么方法可以让我从这种类型的数据集中获得我的结果。
好的...我对您的问题有点困惑。如果您正在计数,产品 5 不应该有 2 的进度吗?或者你只是想得到一个 1,如果有任何正在进行中。
试试这个...
select w1.work as work, count(*) countInProgress
from work w1
inner join work w2 on w1.work = w2.parent
where w2.status = 'In Progress'
group by w1.work
或者,如果您还想包括父项的进行中。
select workItem, count(*) countInProgress from
(
select work, @workItem := if(parent is not null,parent,work) workItem
, status
from work
) t
where status = 'In Progress'
group by workItem
无论哪种方式,问题都有点不清楚。产品 2 如何有一个 wip 任务 "In Progress" 但它本身不在进行中?
构建脚本
CREATE TABLE work (work varchar(20), parent varchar(20), status varchar(20));
INSERT INTO work VALUES
('Product 1',null,'In Progress'),
('Service', 'Product 1','Completed'),
('Delivery', 'Product 1', 'In Progress'),
('Transportation', 'Product 1', 'Completed'),
('Product 2',null,'In Progress'),
('Service', 'Product 2',''),
('Delivery', 'Product 2', 'In Progress'),
('Transportation', 'Product 2', ''),
('Product 3',null,'Completed'),
('Product 4',null,'In Progress'),
('Product 5',null,'In Progress'),
('Delivery', 'Product 5', 'In Progress'),
('Transportation', 'Product 5', 'In Progress')
sqlfiddle link 如果你想玩它... http://sqlfiddle.com/#!9/e0fa02/15 希望这有助于...
评论后编辑:
select w1.work as work
,if(w1.status = 'In Progress' or w2.status = 'In Progress',1,0) countInProgress
from work w1
left join work w2 on w1.work = w2.parent
where w1.parent is null
group by w1.work
试一试...
此外,如果您想查看所有子项目标记为进行中的位置
select w1.work as work
,if(w1.status = 'In Progress' or w2.status = 'In Progress',1,0) countInProgress
,@allInProgress := case when @allInProgress is null and w2.status = 'In Progress' then 1
when @allInProgress is null and (w2.status <> 'In Progress' or w2.status is null) then 0
when w2.status = 'In Progress' then @allInProgress * 1
when w2.status <> 'In Progress' then @allInProgress * 0
end allInProgress
from work w1
left join work w2 on w1.work = w2.parent
where w1.parent is null
group by w1.work
我能够使用两个子查询并通过联合函数组合来实现我的要求。我不确定这是最好的还是唯一的方法,但它给了我想要的并满足了我的要求。
SELECT c.work,
COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) AS inprogress_count
FROM WORK c
INNER JOIN WORK w2 ON c.work = w2.parent
GROUP BY c.work
HAVING COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) <> 0
UNION
SELECT w2.work,
COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) AS inprogress_count
FROM WORK w2
WHERE WORK NOT IN (
SELECT c.work
FROM WORK c
INNER JOIN WORK w2 ON c.work = w2.parent
GROUP BY c.work
HAVING COUNT(
CASE WHEN w2.status = 'In Progress'
THEN '1'
END) <> 0)
AND parent IS NULL
GROUP BY w2.work;