Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 连接Oracle中多行中的多列_Sql_Oracle - Fatal编程技术网

Sql 连接Oracle中多行中的多列

Sql 连接Oracle中多行中的多列,sql,oracle,Sql,Oracle,我在stackoverflow中搜索了这个,找到了,也找到了。但我需要的是将两者结合起来。 我有一张名为komponen的表: id urut tipe c_string c_number op --------------------------------------------- A1 1 S GP NULL * A1 2 N NULL 5 / A1

我在stackoverflow中搜索了这个,找到了,也找到了。但我需要的是将两者结合起来。
我有一张名为komponen的表:

id   urut   tipe   c_string   c_number    op
---------------------------------------------
A1    1      S      GP         NULL        *
A1    2      N      NULL       5           /
A1    3      N      NULL       100         +  //Ignore the last op for each groups.
A2    1      S      GP         NULL        -
A2    2      N      NULL       1000        /  //Ignore the last op for each groups.
预期结果:

id     concat_result
------------------------
A1     GP * 5  /  100
A2     GP - 1000

这可能是使用
listag
groupby
方法。但我不知道如何做到这一点并达到预期的效果。请帮助。

这是一个有一些修改的版本。在
listag

SELECT ID,
   LISTAGG (
      CASE
      WHEN TIPE = 'v' THEN
         C_STRING
      ELSE
         TO_CHAR (C_NUMBER)
      END || OP,
      ' '
   ) WITHIN GROUP (ORDER BY URUT) AS CONCAT_RESULT
FROM KOMPONEN
GROUP BY ID;

这是经过一些修改的。在
listag

SELECT ID,
   LISTAGG (
      CASE
      WHEN TIPE = 'v' THEN
         C_STRING
      ELSE
         TO_CHAR (C_NUMBER)
      END || OP,
      ' '
   ) WITHIN GROUP (ORDER BY URUT) AS CONCAT_RESULT
FROM KOMPONEN
GROUP BY ID;

你的直觉是正确的。在这个解决方案中,我假设c_字符串和c_编号是互斥的,即恰好一个不为空

with t as (
  select id, listagg(nvl(c_string, c_number)||' '||op, ' ') within group (order by urut) result
  from komponen
  group by id
)
select id, substr(result, 1, length(result)-2)
from t;

substr()
的组合将删除最后一个运算符。

您的直觉是正确的。在这个解决方案中,我假设c_字符串和c_编号是互斥的,即恰好一个不为空

with t as (
  select id, listagg(nvl(c_string, c_number)||' '||op, ' ') within group (order by urut) result
  from komponen
  group by id
)
select id, substr(result, 1, length(result)-2)
from t;
substr()
的组合将删除最后一个运算符。

您可以使用:

WITH cte AS
(
   SELECT  
      id,
      LISTAGG(string , '') WITHIN GROUP (ORDER BY urut) AS concat_result
   FROM(SELECT id, urut,
        NVL(c_string, '') || ' ' || NVL(c_number, '') || ' ' ||  NVL(op, '')  AS string
        FROM komponen)
   GROUP BY id
)
SELECT
  id, 
  SUBSTR(concat_result,1,LENGTH(concat_result)-1) AS concat_result
FROM cte
工作原理:

  • 与行
    NVL(c|U字符串,)| | | | | NVL(c|U编号,)| | | | | NVL(op,)
  • 与多行连接
    LISTAGG
  • 删除最后一个字符
    SUBSTR(concat\u结果,1,长度(concat\u结果)-1)
  • 您可以使用:

    WITH cte AS
    (
       SELECT  
          id,
          LISTAGG(string , '') WITHIN GROUP (ORDER BY urut) AS concat_result
       FROM(SELECT id, urut,
            NVL(c_string, '') || ' ' || NVL(c_number, '') || ' ' ||  NVL(op, '')  AS string
            FROM komponen)
       GROUP BY id
    )
    SELECT
      id, 
      SUBSTR(concat_result,1,LENGTH(concat_result)-1) AS concat_result
    FROM cte
    
    工作原理:

  • 与行
    NVL(c|U字符串,)| | | | | NVL(c|U编号,)| | | | | NVL(op,)
  • 与多行连接
    LISTAGG
  • 删除最后一个字符
    SUBSTR(concat\u结果,1,长度(concat\u结果)-1)

  • 一种方法是使用
    row\u number
    分析函数和
    listaggr

    SQL> WITH table_("ID",   urut,   tipe,   c_string,   c_number,    op) AS
      2  (SELECT 'A1', 1, 'S', 'GP', NULL, '*' from dual union all
      3   SELECT 'A1', 2, 'N', NULL, '5', '/' from dual union all
      4   SELECT 'A1', 3, 'N', NULL, '100', '+' from dual union all
      5   SELECT 'A2', 1, 'S', 'GP', NULL, '-' from dual union all
      6   SELECT 'A2', 2, 'N', NULL, '1000', '/' from dual),
      7   -------
      8   -- End if Data preparation
      9   -------
     10  table2_ AS (SELECT t.*, row_number() OVER (partition BY "ID" ORDER BY URUT DESC) AS rn
     11    FROM table_ t)
     12  SELECT "ID",
     13         listagg(coalesce(c_string, c_number) || ' ' || CASE
     14                     WHEN rn = 1 THEN
     15                      NULL
     16                     ELSE
     17                      op
     18                 END,
     19                 ' ') within GROUP(ORDER BY urut) AS EXPR
     20    FROM table2_
     21   GROUP BY "ID"
     22  /
    
    输出

    ID EXPR
    -- --------------------------------------------------------------------------------
    A1 GP * 5 / 100
    A2 GP - 1000
    

    一种方法是使用
    row\u number
    分析函数和
    listaggr

    SQL> WITH table_("ID",   urut,   tipe,   c_string,   c_number,    op) AS
      2  (SELECT 'A1', 1, 'S', 'GP', NULL, '*' from dual union all
      3   SELECT 'A1', 2, 'N', NULL, '5', '/' from dual union all
      4   SELECT 'A1', 3, 'N', NULL, '100', '+' from dual union all
      5   SELECT 'A2', 1, 'S', 'GP', NULL, '-' from dual union all
      6   SELECT 'A2', 2, 'N', NULL, '1000', '/' from dual),
      7   -------
      8   -- End if Data preparation
      9   -------
     10  table2_ AS (SELECT t.*, row_number() OVER (partition BY "ID" ORDER BY URUT DESC) AS rn
     11    FROM table_ t)
     12  SELECT "ID",
     13         listagg(coalesce(c_string, c_number) || ' ' || CASE
     14                     WHEN rn = 1 THEN
     15                      NULL
     16                     ELSE
     17                      op
     18                 END,
     19                 ' ') within GROUP(ORDER BY urut) AS EXPR
     20    FROM table2_
     21   GROUP BY "ID"
     22  /
    
    输出

    ID EXPR
    -- --------------------------------------------------------------------------------
    A1 GP * 5 / 100
    A2 GP - 1000
    

    这给了我
    GP*5/100+
    而不是
    GP*5/100
    。我想忽略每组的最后一个op。我添加了substr以删除最后一个op。谢谢。这给了我
    GP*5/100+
    而不是
    GP*5/100
    。我想忽略每个组的最后一个操作。我添加了substr以删除最后一个操作。谢谢。