對不起,如果這已被問及,搜索了一下,沒有找到像我以後的東西,但如果它有請鏈接。連接來自多個表的多列Oracle
我正在使用Oracle,並試圖在不使用臨時表的多個表中的多個列之間彙總結果。下面的例子:
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
我已經能夠做的事情就像找到誰是在一個特定的項目輸出到一個單一的領域使用的查詢,如用戶使用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
;
(道歉,如果語法稍微偏離在上面,模糊的實際結構和數據的原因,所以這是不是我用活準確的查詢) 這將輸出:
Bob,Joe,Mary
但我想輸出是USERS.USER_NAME和PROJECT_USERS.ACCESS_TYPE類似的格式彙總的組合,也許相隔兩個值 -
Bob-8,Joe-10,Mary-9
我可以得到個人回報
SELECT users.user_name || '-' || project_users.access_type...
返回
Bob-8
Joe-9
Mary-10
並希望那時我能LISTAGG這些結果,但遺憾的是一直沒能到g等它工作。正如前面提到的,臨時表是正確的,因爲我不想進入,但我確信這會讓事情變得更容易。在FROM中使用SELECT不起作用,我不認爲,因爲最後我希望能夠在子查詢中使用它,並且我的理解(以及嘗試它的有限經驗)是不會正確迭代的每次通過。也許我錯了,只是做錯了。
有什麼建議嗎?
謝謝!
這豈不實際上是更容易,因爲你在一個列表做返回的數據,而不是逗號分隔值,如果你要在一個子查詢中使用它? – BobC
@BobC會是理想的,但是這個被用於報告的報告笨拙地被用來解析數據並將其重新輸入到別的東西中。這真的很難看,而不是我會怎麼做,但最終我需要子查詢返回一行來用於此。顯然,正確的數據完整性標準應該被詛咒。 – CascadeOverflow