<legend id='wUEgU'><style id='wUEgU'><dir id='wUEgU'><q id='wUEgU'></q></dir></style></legend>
  • <small id='wUEgU'></small><noframes id='wUEgU'>

        <bdo id='wUEgU'></bdo><ul id='wUEgU'></ul>

        <i id='wUEgU'><tr id='wUEgU'><dt id='wUEgU'><q id='wUEgU'><span id='wUEgU'><b id='wUEgU'><form id='wUEgU'><ins id='wUEgU'></ins><ul id='wUEgU'></ul><sub id='wUEgU'></sub></form><legend id='wUEgU'></legend><bdo id='wUEgU'><pre id='wUEgU'><center id='wUEgU'></center></pre></bdo></b><th id='wUEgU'></th></span></q></dt></tr></i><div id='wUEgU'><tfoot id='wUEgU'></tfoot><dl id='wUEgU'><fieldset id='wUEgU'></fieldset></dl></div>
        <tfoot id='wUEgU'></tfoot>
      1. Oracle 将行转为列

        时间:2023-06-06
          <tbody id='zzpJT'></tbody>
          <bdo id='zzpJT'></bdo><ul id='zzpJT'></ul>

            <small id='zzpJT'></small><noframes id='zzpJT'>

            • <i id='zzpJT'><tr id='zzpJT'><dt id='zzpJT'><q id='zzpJT'><span id='zzpJT'><b id='zzpJT'><form id='zzpJT'><ins id='zzpJT'></ins><ul id='zzpJT'></ul><sub id='zzpJT'></sub></form><legend id='zzpJT'></legend><bdo id='zzpJT'><pre id='zzpJT'><center id='zzpJT'></center></pre></bdo></b><th id='zzpJT'></th></span></q></dt></tr></i><div id='zzpJT'><tfoot id='zzpJT'></tfoot><dl id='zzpJT'><fieldset id='zzpJT'></fieldset></dl></div>
              <tfoot id='zzpJT'></tfoot>

                <legend id='zzpJT'><style id='zzpJT'><dir id='zzpJT'><q id='zzpJT'></q></dir></style></legend>

                1. 本文介绍了Oracle 将行转为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有以下表格:

                  TABLE1
                  ------
                  id name
                  1  n1
                  2  n2
                  
                  
                  TABLE2
                  ------
                  id tipo valor
                  1  t1   v1
                  1  t2   v2
                  2  t1   v1
                  2  t2   v5
                  2  t3   v3
                  

                  我正在努力:

                  id name t1 t2 t3
                  1  n1   v1 v2 -
                  2  n2   v1 v5 v3
                  

                  有可能吗?我一直在寻找示例,但仍然没有弄清楚.

                  Is it possible? I have been looking examples but still I don't get nothing clear.

                  谢谢!

                  推荐答案

                  一种选择是使用 PIVOT Clause

                  One option would be using PIVOT Clause

                  SELECT *
                    FROM (SELECT t1.*, t2.tipo, t2.valor 
                            FROM table1 t1 
                            JOIN table2 t2 
                              ON t2.id = t1.id)
                   PIVOT 
                   (
                    MAX(valor) FOR tipo IN ( 't1' AS "t1", 't2' AS "t2", 't3' AS "t3" )
                   )  
                  

                  但这是静态的,例如.需要更新 tipo 列的不同值.为了使其动态化,您可以创建一个函数

                  but this is static, eg. needs to be updated as different values for tipo column is added. In order to make it dynamic, you can create a function

                  CREATE OR REPLACE FUNCTION Get_Pivoted_Cols RETURN SYS_REFCURSOR IS
                    v_recordset SYS_REFCURSOR;
                    v_sql       VARCHAR2(32767); 
                    v_cols      VARCHAR2(32767);      
                  BEGIN
                    SELECT LISTAGG( ''''||tipo||''' AS "'||tipo||'"' , ',' )
                            WITHIN GROUP ( ORDER BY tipo )  
                      INTO v_cols
                      FROM ( SELECT DISTINCT tipo FROM table2 );
                  
                    v_sql := 'SELECT *
                                FROM (SELECT t1.*, t2.tipo, t2.valor 
                                        FROM table1 t1 
                                        JOIN table2 t2 
                                          ON t2.id = t1.id)
                               PIVOT 
                               (
                                MAX(valor) FOR tipo IN ( '|| v_cols ||' )
                               )';   
                  
                    OPEN v_recordset FOR v_sql;
                    RETURN v_recordset;
                  END;
                  /
                  

                  然后使用调用

                  VAR rc REFCURSOR
                  EXEC :rc := Get_Pivoted_Cols;
                  PRINT rc
                  

                  来自 SQL 开发人员的控制台.

                  from SQL developer's console.

                  演示

                  这篇关于Oracle 将行转为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SCOPE_IDENTITY 似乎不适用于参数化查询 下一篇:SQL 平均(计数(*))?

                  相关文章

                  最新文章

                    <bdo id='7hSCw'></bdo><ul id='7hSCw'></ul>

                  <small id='7hSCw'></small><noframes id='7hSCw'>

                  <legend id='7hSCw'><style id='7hSCw'><dir id='7hSCw'><q id='7hSCw'></q></dir></style></legend>
                  1. <i id='7hSCw'><tr id='7hSCw'><dt id='7hSCw'><q id='7hSCw'><span id='7hSCw'><b id='7hSCw'><form id='7hSCw'><ins id='7hSCw'></ins><ul id='7hSCw'></ul><sub id='7hSCw'></sub></form><legend id='7hSCw'></legend><bdo id='7hSCw'><pre id='7hSCw'><center id='7hSCw'></center></pre></bdo></b><th id='7hSCw'></th></span></q></dt></tr></i><div id='7hSCw'><tfoot id='7hSCw'></tfoot><dl id='7hSCw'><fieldset id='7hSCw'></fieldset></dl></div>
                  2. <tfoot id='7hSCw'></tfoot>