2

SQL 新手,并且有一个查询通过多个表的多个连接提取一些数据。

样本数据:

PERSON_NAME FUNCTION_NAME  FUNCTION_ID  FUNCTION_GROUP

Bob         View Result    1            Editor
Bob         Edit Result    4            Editor
Bob         Delete Result  3            Editor
Bob         Email Result   8            Editor
Mary        Print Letter   45           Admin
Mary        Grant Access   37           Admin

函数有 ID,function_groups 有很多函数。我想查询数据,而不是看起来类似于上面的示例,它看起来像:

PERSON_NAME FUNCTION_NAME                             FUNCTION_ID     FUNCTION_GROUP

Bob         View Result,Edit Result, Delete Result   1,4,3,8          Editor
Mary        Print Letter,Grant Access                 45,37           Admin

“Bob 属于编辑器,编辑器具有以下功能”作为一个结果,而不是最初的示例,其中返回多行。

我认为独特或独特的关键字可以帮助我是否正确?谢谢!

编辑:现在有代码

select staff_member.person_name, function.function_name,staff_group_function.function_id, staff_group.function_group_name
 from staff_member
 inner join staff_group
 on staff_group.staff_group_id=staff_group_member.staff_group_id
 inner join staff_group_function
 on staff_group_function.staff_group_id=staff_group_member.staff_group_id
 inner join function
 on function.function_id=staff_group_function.function_group_name
4

3 回答 3

4

没有。你需要的是LISTAGG()如果你有 Oracle 11g2。这可能是您的查询:

SELECT person_name,
       LISTAGG(function_name, ', ') WITHIN GROUP (ORDER BY 1) function_name,
       LISTAGG(function_id,   ', ') WITHIN GROUP (ORDER BY 1) function_id,
       function_group
FROM my_table
GROUP BY person_name, function_group

或者(根据您的最新评论):

SELECT person_name,
       LISTAGG(function_name,  ', ') WITHIN GROUP (ORDER BY 1) function_name,
       LISTAGG(function_id,    ', ') WITHIN GROUP (ORDER BY 1) function_id,
       LISTAGG(function_group, ', ') WITHIN GROUP (ORDER BY 1) function_group
FROM my_table
GROUP BY person_name

对于 11g2 之前的任何版本,这篇有趣的文章为您提供了解决方案:

http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php

于 2012-04-11T14:37:31.170 回答
2

UNIQUE并且DISTINCT与分组和汇总结果无关。您必须GROUP BY PERSON_NAME然后FUNCTION_GROUP应用您自己的聚合函数来对剩余的结果进行分组。

本文准确地告诉您如何通过 Oracle 中的聚合函数使用LISTAGG.

于 2012-04-11T14:42:41.763 回答
1

10g的工作原理如下

with tab as (
 select 'Bob' PERSON_NAME,'View Result' FUNCTION_NAME,'1' FUNCTION_ID,'Editor' FUNCTION_GROUP      from dual
union all
select 'Bob' PERSON_NAME,'Edit Result' FUNCTION_NAME,'4' FUNCTION_ID,'Editor' FUNCTION_GROUP from    dual
union all
select 'Bob' PERSON_NAME ,'Delete Result'FUNCTION_NAME,'3' FUNCTION_ID,'Editor' FUNCTION_GROUP from dual
union all
select 'Bob' PERSON_NAME,'Email Result' FUNCTION_NAME,'8' FUNCTION_ID,'Editor' FUNCTION_GROUP from dual
union all
select 'Mary' PERSON_NAME,'Print Letter' FUNCTION_NAME,'45' FUNCTION_ID,'Admin' FUNCTION_GROUP from dual
union all
select 'Mary' PERSON_NAME , 'Grant Access' FUNCTION_NAME ,'37' FUNCTION_ID,'Admin' FUNCTION_GROUP from dual
 )
select person_name
      ,wm_concat(function_name) function_name
      ,wm_concat(function_id) function_id
      ,function_group
 from tab
 group by person_name,function_group

输出 输出

于 2012-04-11T17:31:43.653 回答