简体   繁体   English

Oracle SQL-将来自多个表的结果集中的同一列中的值合并

[英]Oracle SQL - Combine values in the same column in one column in the result set from multiple table

I would liek to know how to concatnate several records from the same column from two different table in one. 我很想知道如何合并来自两个不同表的同一列中的多个记录。 for instance below are my tables: 例如下面是我的表:

NOTE TABLE: 注释表:

INVOICE NOTES
1000    REPLACE PUMP
1000    REPLACE OIL
1000    REPLACE FILTER
1111    WO# 123
1111    REPLACE GASKET
1234    REPLACE OIL

INVOICE TABLE: 发票表:

INVOICE AMOUNT
1000    100
1111    50
1234    20

I can run this query and the result: 我可以运行此查询和结果:

SELECT INV.INVOICE, INV.AMOUNT FROM INVOICE INV
INNER JOIN NOTES ON INV.INVOICE = NOTES.INVOICE

but I would like to combine the Notes for each invoice in one column in my result set. 但我想将每个发票的注释合并到结果集中的一栏中。 For examples my result should look like this 例如,我的结果应如下所示

INVOICE  AMOUNT     NOTES
1000     100        REPLACE PUMP, REPLACE OIL, REPLACE FILTER
1111     50         WO# 123,REPLACE GASKET
1234     20         REPLACE OIL

documentation - listagg 文档-listagg

query 询问

with inv_notes as
(select 1000 as INVOICE, 'REPLACE PUMP' as notes from dual union all
select 1000, 'REPLACE PUMP' from dual union all
select 1000, 'REPLACE OIL' from dual union all
select 1000, 'REPLACE FILTER' from dual union all
select 1111, 'WO# 123' from dual union all
select 1111, 'REPLACE GASKET' from dual union all
select 1234, 'REPLACE OIL' from dual
) 
,
inv_amount as (
select 1000 as INVOICE, 100 AMOUNT from dual union all
select 1111, 50 from dual union all
select 1234, 20 from dual)
select a.invoice, a.amount, listagg(n.notes, ',') WITHIN GROUP (ORDER BY a.invoice, a.amount) notes
from inv_notes n
 inner join inv_amount a on n.invoice = a.invoice
group by a.invoice, a.amount 

result 结果

1   1000    100 REPLACE FILTER,REPLACE OIL,REPLACE PUMP,REPLACE PUMP
2   1111    50  REPLACE GASKET,WO# 123
3   1234    20  REPLACE OIL

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

相关问题 oracle sql中多列表中一列的唯一值 - Unique values of one column in a multiple column table in oracle sql 将多个列值合并为一个并在多行中打印一个字符串-Oracle SQL - Combine multiple column values to one and print the one string in multiple rows - oracle SQL Oracle SQL-查询具有相同列的2个值的表 - Oracle SQL - Querying a table with 2 values for the same column sql从同一表和同一列中的值获取到一行 - sql getting values from the same table and same column into one row SQL 服务器在同一张表上将多列合并为1列 - SQL Server combine multiple column into 1 column on the same table ORACLE SQL:根据另一列中的相同键值选择基于一列中不同值的多行 - ORACLE SQL: select multiple rows basd on different values in one column for a same key value in another column 将同一表中一列的值更新到 SQL Server 中的另一列 - Update values from one column in same table to another in SQL Server 一列的SQL集值等于同一表中另一列的值 - SQL set values of one column equal to values of another column in the same table SQL:我们能够将多个条件组合到一个结果列中吗? - SQL: Are we able to combine multiple conditions into one result column? SQL DISTINCT在除一列以外的多列上,合并结果中的最后一列 - SQL DISTINCT on multiple columns except one, combine last column in result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM