我有下表,其中包含工作活动及其任务:
TASK_ID | TASK_TYPE_ID | TASK_NAME | ACT_ID
========+==============+===========+========
100000 | 101 | A | 300
100001 | 102 | B | 300
100002 | 103 | C | 300
100004 | 101 | A | 310
100005 | 103 | C | 310
100006 | 101 | A | 312
100007 | 103 | C | 312
100008 | 101 | A | 314
100009 | 102 | B | 314
可以看到,在此表中有四个活动(300,310,312,314),但有三个不同的过程。这里假设过程是不同的任务序列。第一过程= A + B + C序列(用于活动300),第二过程= A + C序列(用于活动310和312),第四过程= A + B.也可能有其他活动(使用)相同的过程。
我需要SQL来从这样的表中提取所有进程或进程类型(不同的任务序列)。
结果输出应如下所示:
PROCESS | PROCESS_TASKS_SEQUENCE
========+=======================
1 | A,B,C
2 | A,C
3 | A,B
从输入数据中提取三个不同的过程,第一个过程由任务A + B + C组成,第二个过程由任务A + C组成,第三个过程由任务A + B组成。
答案 0 :(得分:0)
这就像你之后的那样吗?
with sample_data as (select 100000 task_id, 101 task_type_id, 'Task A' task_name, 300 act_id from dual union all
select 100001 task_id, 102 task_type_id, 'Task B' task_name, 300 act_id from dual union all
select 100002 task_id, 103 task_type_id, 'Task C' task_name, 300 act_id from dual union all
select 100004 task_id, 101 task_type_id, 'Task A' task_name, 310 act_id from dual union all
select 100005 task_id, 103 task_type_id, 'Task C' task_name, 310 act_id from dual union all
select 100006 task_id, 101 task_type_id, 'Task A' task_name, 312 act_id from dual union all
select 100007 task_id, 103 task_type_id, 'Task C' task_name, 312 act_id from dual union all
select 100008 task_id, 101 task_type_id, 'Task A' task_name, 314 act_id from dual union all
select 100009 task_id, 102 task_type_id, 'Task B' task_name, 314 act_id from dual)
-- end of mimicking a table called sample_data which contains ... sample data. See SQL below:
select distinct task_name,
dense_rank() over (order by length(task_list) desc, task_list) grp_num
from (select task_id,
task_type_id,
task_name,
act_id,
listagg(task_type_id, ',') within group (order by task_type_id) over (partition by act_id) task_list
from sample_data)
order by grp_num, task_name;
TASK_NAME GRP_NUM
--------- ----------
Task A 1
Task B 1
Task C 1
Task A 2
Task B 2
Task A 3
Task C 3
答案 1 :(得分:0)
Oracle 11g R2架构设置:
CREATE TABLE table_name ( TASK_ID, TASK_TYPE_ID, TASK_NAME, ACT_ID ) AS
SELECT 100000, 101, 'A', 300 FROM DUAL
UNION ALL SELECT 100001, 102, 'B', 300 FROM DUAL
UNION ALL SELECT 100002, 103, 'C', 300 FROM DUAL
UNION ALL SELECT 100004, 101, 'A', 310 FROM DUAL
UNION ALL SELECT 100005, 103, 'C', 310 FROM DUAL
UNION ALL SELECT 100006, 101, 'A', 312 FROM DUAL
UNION ALL SELECT 100007, 103, 'C', 312 FROM DUAL
UNION ALL SELECT 100008, 101, 'A', 314 FROM DUAL
UNION ALL SELECT 100009, 102, 'B', 314 FROM DUAL
UNION ALL SELECT 100010, 102, 'B', 316 FROM DUAL
UNION ALL SELECT 100011, 101, 'A', 316 FROM DUAL
查询1 :
SELECT ROWNUM AS PROCESS,
PROCESS_TASK_SEQUENCE
FROM (
SELECT DISTINCT
LISTAGG( TASK_NAME, ',' ) WITHIN GROUP ( ORDER BY TASK_ID ) AS PROCESS_TASK_SEQUENCE
FROM table_name
GROUP BY ACT_ID
)
<强> Results 强>:
| PROCESS | PROCESS_TASK_SEQUENCE |
|---------|-----------------------|
| 1 | A,B,C |
| 2 | A,B |
| 3 | A,C |
| 4 | B,A |