1. <small id='BJXmr'></small><noframes id='BJXmr'>

    1. <tfoot id='BJXmr'></tfoot>
        <bdo id='BJXmr'></bdo><ul id='BJXmr'></ul>

      <i id='BJXmr'><tr id='BJXmr'><dt id='BJXmr'><q id='BJXmr'><span id='BJXmr'><b id='BJXmr'><form id='BJXmr'><ins id='BJXmr'></ins><ul id='BJXmr'></ul><sub id='BJXmr'></sub></form><legend id='BJXmr'></legend><bdo id='BJXmr'><pre id='BJXmr'><center id='BJXmr'></center></pre></bdo></b><th id='BJXmr'></th></span></q></dt></tr></i><div id='BJXmr'><tfoot id='BJXmr'></tfoot><dl id='BJXmr'><fieldset id='BJXmr'></fieldset></dl></div>
      <legend id='BJXmr'><style id='BJXmr'><dir id='BJXmr'><q id='BJXmr'></q></dir></style></legend>
    2. 动态旋转表 Oracle

      时间:2023-09-20
      <i id='L28dr'><tr id='L28dr'><dt id='L28dr'><q id='L28dr'><span id='L28dr'><b id='L28dr'><form id='L28dr'><ins id='L28dr'></ins><ul id='L28dr'></ul><sub id='L28dr'></sub></form><legend id='L28dr'></legend><bdo id='L28dr'><pre id='L28dr'><center id='L28dr'></center></pre></bdo></b><th id='L28dr'></th></span></q></dt></tr></i><div id='L28dr'><tfoot id='L28dr'></tfoot><dl id='L28dr'><fieldset id='L28dr'></fieldset></dl></div>
        <tbody id='L28dr'></tbody>

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

        1. <legend id='L28dr'><style id='L28dr'><dir id='L28dr'><q id='L28dr'></q></dir></style></legend>
        2. <tfoot id='L28dr'></tfoot>
            • <bdo id='L28dr'></bdo><ul id='L28dr'></ul>

                本文介绍了动态旋转表 Oracle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一张看起来像这样的表格:

                I have a table that looks like this:

                C_ID   P_ID   KEY    VALUE
                null   null   KEY1   VALUE1
                null   null   KEY2   VALUE2
                null   null   KEY3   VALUE3
                2       2     KEY4   VALUE4
                2       3     KEY5   VALUE5
                

                我想得到这个结果表/视图:

                I want to get this result table/view:

                C_ID   P_ID   KEY1    KEY2    KEY3      KEY4    KEY5
                NULL   NULL  VALUE1  VALUE2  VALUE3     NULL    NULL
                2       2     NULL    NULL    NULL     VALUE4   NULL
                2       3     NULL    NULL    NULL      NULL   VALUE5
                

                有人知道我如何实现这一目标吗?我已经试过了:

                Has anybody an Idea how I could achieve this? I have tried it with:

                select * from (select c_id, p_id, r_key, r_value from s_projectroles) pivot (max(r_value) for r_key in (any));
                

                我有一个错误:

                ORA-00936: Ausdruck fehlt
                00936. 00000 -  "missing expression"
                

                推荐答案

                这可以通过以下方式动态完成.首先,这里是查询的静态版本,因此您可以看到最终的 sql:

                This can be done dynamically the following way. First, here is the static version of the query so you can see the final sql:

                select c_id,
                  p_id,
                  max(case when r_key= 'KEY1' then r_value  end) KEY1,
                  max(case when r_key= 'KEY2' then r_value  end) KEY2,
                  max(case when r_key= 'KEY3' then r_value  end) KEY3,
                  max(case when r_key= 'KEY4' then r_value  end) KEY4,
                  max(case when r_key= 'KEY5' then r_value  end) KEY5
                from s_projectroles
                group by c_id, p_id
                

                参见SQL Fiddle with Demo

                然后要动态执行此操作,您可以创建以下过程:

                Then to do this dynamically, you can create the following procedure:

                CREATE OR REPLACE procedure dynamic_pivot(p_cursor in out sys_refcursor)
                as
                    sql_query varchar2(1000) := 'select c_id, P_id ';
                
                    begin
                        for x in (select distinct r_key from s_projectroles order by 1)
                        loop
                            sql_query := sql_query ||
                              ' , max(case when r_key = '''||x.r_key||''' then r_value end) as '||x.r_key;
                
                                dbms_output.put_line(sql_query);
                        end loop;
                
                        sql_query := sql_query || ' from s_projectroles group by c_id, p_id';
                
                        open p_cursor for sql_query;
                    end;
                /
                

                然后执行它:

                variable x refcursor
                exec dynamic_pivot(:x)
                print x
                

                结果是一样的:

                |   C_ID |   P_ID |   KEY1 |   KEY2 |   KEY3 |   KEY4 |   KEY5 |
                ----------------------------------------------------------------
                | (null) | (null) | VALUE1 | VALUE2 | VALUE3 | (null) | (null) |
                |      2 |      2 | (null) | (null) | (null) | VALUE4 | (null) |
                |      2 |      3 | (null) | (null) | (null) | (null) | VALUE5 |
                

                这篇关于动态旋转表 Oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PLS-00428:此 SELECT 语句中需要 INTO 子句 下一篇:Sql*plus 总是返回退出代码 0?

                相关文章

                  <tfoot id='jHx5C'></tfoot>

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

                  1. <legend id='jHx5C'><style id='jHx5C'><dir id='jHx5C'><q id='jHx5C'></q></dir></style></legend>