【问题标题】:Oracle SQL Concatenate multiple entries with different conditionsOracle SQL 连接具有不同条件的多个条目
【发布时间】:2022-01-11 20:47:00
【问题描述】:

我有两张桌子

第一个表:

Row ID1 ID2 ID3
1 1 2 3
2 4 5 6

第二个表:

ID Text
1 Text1
2 Text2
3 Text3
4 Text4
5 Text5
6 Text6

期望得到这样的视图

Row ConcatinatedText
1 text1 text2 text3
2 text4 text5 text6

您知道我如何通过 oracle 中的语句来实现这一目标吗? 这就是我到目前为止所拥有的一切......但我不知道我该如何继续...... 我以为我可以在连接中使用 Alias..但我不知道如何使用这些来通过不同的 ID 获取文本...

select ID1text || ' ' || ID2text|| ' ' || ID3text||' '||
from firstTable join secondTable

提前谢谢你:)

【问题讨论】:

  • 你必须加入 secondTable 3 次,每个 id 列一次。
  • ... 或使用三个 子查询 并将它们连接起来。
  • 感谢您的快速答复。但是你能不能给我一个例子,如何在连接中使用这个别名来处理这个文本?这样我就可以在连接中使用它了吗?我真的不知道:/

标签: sql oracle join concatenation


【解决方案1】:

如果您不想三次加入 secondTable,您可以使用以下替代方法,即使用 Unpivoting 子句首先取消旋转 firstTable(列 ID* --> 行),然后将其与 secondTable 连接。在这一步之后,您要做的就是将结果按“行”列分组,然后使用 Listagg 聚合函数根据需要连接文本。

with firstTable_Unpivoted ( "Row", ID, source_column ) as (
    SELECT "Row", ID, source_column
    from firstTable
    unpivot (
        ID for source_column in (
            ID1 as 'ID1'
        ,   ID2 as 'ID2'
        ,   ID3 as 'ID3'
        )
    )
)
select 
    FTU."Row"
  , listagg(ST.Text, ' ') within group (order by FTU.ID) ConcatinatedText
from 
    firstTable_Unpivoted FTU
left join 
    secondTable ST
        on FTU.ID = ST.ID
group by FTU."Row"
;

demo on db<>fiddle

【讨论】:

    【解决方案2】:

    对于 small 表,您可以安全地使用 子查询 来获取每个ID 的文本值并连接结果。

    示例数据示例

    with t1 as (
    select 1 id, 1 id1, 2 id2, 3 id3 from dual union all
    select 2,4,5,6 from dual),
    t2 as (
    select rownum id, 'Text'||rownum text from dual connect by level <= 6)
    /* Your Query */
    select 
     id,
     (select text from t2 where id = t1.id1)||' '||
     (select text from t2 where id = t1.id2)||' '|| 
     (select text from t2 where id = t1.id3) as  concTxt
    from t1;
    
            ID CONCTXT                                                                                                                               
    ---------- --------------------- 
             1 Text1 Text2 Text3                                                                                                                     
             2 Text4 Text5 Text6 
    

    如评论中所述,否则使用三个连接,这会产生相同的结果,对于更大的数据可能性能更高。

    with t1 as (
    select 1 id, 1 id1, 2 id2, 3 id3 from dual union all
    select 2,4,5,6 from dual),
    t2 as (
    select rownum id, 'Text'||rownum text from dual connect by level <= 6)
    /* Your Query */
    select 
     t1.id,
     t21.text ||' ' || t22.text || ' ' || t23.text as concTxt
    from t1
    join t2 t21 on t1.id1 = t21.id
    join t2 t22 on t1.id2 = t22.id
    join t2 t23 on t1.id3 = t23.id
    

    【讨论】:

    • 非常感谢。我稍后会测试它:)
    • 感谢它在 Oracle 中运行良好。您知道是否可以使用 JPA 编写此查询?
    • 肯定可以在 JPA 中使用。使用 names query 或将逻辑封装在 database view 中并在 JPA 中查询。
    【解决方案3】:

    以下查询有点动态,只需要在过滤条件中添加您需要的列数:

    T11: T12:

    SELECT   row_id
            ,LISTAGG( text, ',' ) WITHIN GROUP (ORDER BY id)
    FROM     (WITH
                  aa
                  AS
                      (SELECT ids
                             ,row_id
                       FROM   t11 UNPIVOT (ids FOR id1 IN (id1, id2, id3)))
              SELECT *
              FROM   aa
                    ,t12
              WHERE  aa.ids = t12.id)
    GROUP BY row_id
        
    

    【讨论】:

      猜你喜欢
      • 2014-04-05
      • 2012-05-12
      • 1970-01-01
      • 2014-03-11
      • 2010-11-10
      • 2020-06-26
      • 2011-08-15
      • 2022-10-14
      • 1970-01-01
      相关资源
      最近更新 更多