如何在oracle中连接一个字段中的行,查询多个表
How to concatenate rows in one field in oracle, with a query over mutliple tables
这是在 oracle 数据库中。因此使用 listagg....
我有一个客户 table 和一个资产 table。我需要能够 return 资产中的 IP 地址 table 用逗号分隔。每个客户可以有 2 到 30 个(左右)IP。
资产
cust_id IP
ABCD 192.168.1.5
ABCD 192.168.1.6
ABCD 192.168.1.7
DEFG 192.168.10.1
DEFG 192.168.10.2
客户
Cust_id Cust_name
DEFG My first customer
ABCD My second Customer
我需要return这个:
My First Customer DEFG 192.168.10.1, 192.168.10.2
My second customer ABCD 192.168.1.5, 192.168.1.6, 192.168.1.7
一个简单的连接查询非常适合 return 单独的行:
select
CUST_NAMES.CUST_ID as CUST_ID, CUST_NAMES.CUST_NAME, IP
from CUST_NAMES
INNER JOIN ASSETS
ON CUST_NAMES.CUST_ID = ASSETS.CUST_ID
listagg 在单个 table 查询上表现出色:
select ASSETS.CUST_ID, listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS
ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by ASSETS.CUST_ID
但是,当我尝试将客户名称添加到查询中时,它告诉我这不是 GROUP BY 函数
select CUSTOMERS.CUST_NAME, ASSETS.CUST_ID, listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS
ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by ASSETS.CUST_ID
select 列表中的所有非聚合列都必须包含在 group by
子句中,因此您也需要添加 CUSTOMERS.CUST_NAME
。
select CUSTOMERS.CUST_NAME, ASSETS.CUST_ID,
listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS
ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by CUSTOMERS.CUST_NAME, ASSETS.CUST_ID
这与涉及的两个表没有任何关系。
由于每个客户 ID 只有一个客户(姓名)(我猜),我认为没有理由不简单地将 CUST_NAME 添加到您的 GROUP BY:
select CUSTOMERS.CUST_NAME, ASSETS.CUST_ID, listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by ASSETS.CUST_ID, CUSTOMERS.CUST_NAME
这是在 oracle 数据库中。因此使用 listagg....
我有一个客户 table 和一个资产 table。我需要能够 return 资产中的 IP 地址 table 用逗号分隔。每个客户可以有 2 到 30 个(左右)IP。
资产
cust_id IP
ABCD 192.168.1.5
ABCD 192.168.1.6
ABCD 192.168.1.7
DEFG 192.168.10.1
DEFG 192.168.10.2
客户
Cust_id Cust_name
DEFG My first customer
ABCD My second Customer
我需要return这个:
My First Customer DEFG 192.168.10.1, 192.168.10.2
My second customer ABCD 192.168.1.5, 192.168.1.6, 192.168.1.7
一个简单的连接查询非常适合 return 单独的行:
select
CUST_NAMES.CUST_ID as CUST_ID, CUST_NAMES.CUST_NAME, IP
from CUST_NAMES
INNER JOIN ASSETS
ON CUST_NAMES.CUST_ID = ASSETS.CUST_ID
listagg 在单个 table 查询上表现出色:
select ASSETS.CUST_ID, listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS
ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by ASSETS.CUST_ID
但是,当我尝试将客户名称添加到查询中时,它告诉我这不是 GROUP BY 函数
select CUSTOMERS.CUST_NAME, ASSETS.CUST_ID, listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS
ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by ASSETS.CUST_ID
select 列表中的所有非聚合列都必须包含在 group by
子句中,因此您也需要添加 CUSTOMERS.CUST_NAME
。
select CUSTOMERS.CUST_NAME, ASSETS.CUST_ID,
listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS
ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by CUSTOMERS.CUST_NAME, ASSETS.CUST_ID
这与涉及的两个表没有任何关系。
由于每个客户 ID 只有一个客户(姓名)(我猜),我认为没有理由不简单地将 CUST_NAME 添加到您的 GROUP BY:
select CUSTOMERS.CUST_NAME, ASSETS.CUST_ID, listagg(IP, ',') within group (order by ASSETS.CUST_ID)
from ASSETS
INNER JOIN CUSTOMERS ON CUSTOMERS.CUST_ID = ASSETS.CUST_ID
group by ASSETS.CUST_ID, CUSTOMERS.CUST_NAME