简体   繁体   English

连接来自多个表的多个列Oracle

[英]concatenate multiple columns from multiple tables Oracle

Sorry if this has been asked, searched around a bit and didn't find something quite like what I am after, but if it has please link. 很抱歉,是否有人问过这个问题,搜索了一下,没有找到我想要的东西,但是如果有,请链接。

I am using Oracle and trying to aggregate a result across multiple columns in multiple tables without using a temp table. 我正在使用Oracle,并尝试在不使用临时表的情况下跨多个表中的多个列聚合结果。 Example below: 下面的例子:

Table: USERS
--------------
ID | USER_NAME
--------------
1  | Bob
2  | Joe
3  | Mary

Table: PROJECT_USERS
----------------------------------
USER_ID | PROJECT_ID | ACCESS_TYPE
----------------------------------
1       |123         |8
1       |456         |9
1       |789         |10
2       |123         |10
2       |456         |9
2       |789         |8
3       |123         |9
3       |456         |10
3       |789         |10

I've been able to use LISTAGG for some success in doing things like finding the users who are in a particular project output into a single field using a query like: 我已经能够使用LISTAGG取得一些成功,例如使用类似查询的方式来查找特定项目输出中的用户到单个字段中:

SELECT
    LISTAGG(users.user_name, ',') WITHIN GROUP (ORDER BY users.user_name)
FROM
    users,
    project_users
WHERE
    project_users.user_id = users.id
    AND project_users.project_id = 123
GROUP BY ID
;

(apologies if the syntax is slightly off in the above, obscuring the actual structure and data for reasons so this isn't the exact query I'm using live) Which would output: (很抱歉,如果语法在上面略有不同,由于某种原因遮盖了实际的结构和数据,因此这不是我正在使用的确切查询),它将输出:

Bob,Joe,Mary

But what I'd like for output would be a combination of USERS.USER_NAME and PROJECT_USERS.ACCESS_TYPE aggregated in a similar format, perhaps with the two values separated by - 但是我想要输出的是将USERS.USER_NAME和PROJECT_USERS.ACCESS_TYPE组合在一起以类似的格式聚合,也许两个值之间用-分隔

Bob-8,Joe-10,Mary-9

I can get the individual returns 我可以获得个人收益

SELECT users.user_name || '-' || project_users.access_type...

Returning 归来

Bob-8
Joe-9
Mary-10

And was hoping I could then LISTAGG those results, but unfortunately haven't been able to get it to work. 并希望我能LISTAGG获得这些结果,但不幸的是未能将其发挥作用。 As mentioned earlier, a temp table is right out, for reasons I don't want to get into, though I'm sure that would make things a lot easier. 正如前面提到的,出于我不想进入的原因,临时表是正确的,尽管我确信这会使事情变得容易得多。 Using a SELECT in FROM won't work, I don't think, as in the end I'd like to be able to use this in subqueries, and my understanding (and limited experience in trying it) is it wont iterate properly for each pass. 我认为,在FROM中使用SELECT是行不通的,因为最终我希望能够在子查询中使用它,而我的理解(尝试的经验有限)会不会为每次通过。 Maybe I'm wrong, though, and just did it wrong. 不过,也许我错了,只是做错了。

Any suggestions? 有什么建议么?

Thanks! 谢谢!

It seems you want something like this. 看来您想要这样的东西。 Not sure why you want it (in this case), but it illustrates the method you were asking about. 不知道为什么要这样做(在这种情况下),但是它说明了您所要求的方法。

with
     users ( id, user_name ) as (
       select 1, 'Bob'  from dual union all
       select 2, 'Joe'  from dual union all
       select 3, 'Mary' from dual
     ),
     project_users ( user_id, project_id, access_type ) as (
       select 1, 123,  8 from dual union all
       select 1, 456,  9 from dual union all
       select 1, 789, 10 from dual union all
       select 2, 123, 10 from dual union all
       select 2, 456,  9 from dual union all
       select 2, 789,  8 from dual union all
       select 3, 123,  9 from dual union all
       select 3, 456, 10 from dual union all
       select 3, 789, 10 from dual
     )
-- End of test data (not part of the SQL query). 
-- Query begins below this line.
select project_id,
       listagg(user_name || '-' || to_char(access_type), ',')
           within group (order by user_name) as comma_sep_list
from   users u join project_users p on u.id = p.user_id
group by project_id
;

PROJECT_ID COMMA_SEP_LIST
---------- --------------------
       123 Bob-8,Joe-10,Mary-9
       456 Bob-9,Joe-9,Mary-10
       789 Bob-10,Joe-8,Mary-10

3 rows selected.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM