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

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

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

      • <bdo id='DnoFD'></bdo><ul id='DnoFD'></ul>

        Oracle中如何将行转换为列?

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

        • <tfoot id='KVan4'></tfoot>
          <legend id='KVan4'><style id='KVan4'><dir id='KVan4'><q id='KVan4'></q></dir></style></legend>

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

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

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

                  问题描述

                  我有一个这种形式的表格(这只是部分视图,表格包含 100 多列).

                  I have a table in this form (this is just the partial view, the table contains more than 100 columns).

                   LOAN NUMBER   DOCUMENT_TYPE                DOCUMENT_ID
                   992452533663  Voters ID                    XPD0355636
                   992452533663  Pan card                     CHXPS5522D
                   992452533663  Drivers licence              DL-0420110141769
                  

                  一个贷款号,我有三种文件作为证明.我希望将这些详细信息转换为列并采用以下形状:

                  For a single loan number, I have three kinds of documents as proof. I want these details to be converted into columns and take the following shape:

                  LOAN NUMBER     VOTERS_ID    PAN_CARD     DRIVERS LICENCE
                  992452533663    XPD0355636   CHXPS5522D   DL-0420110141769
                  

                  如何解决这个问题?

                  推荐答案

                  如果您使用的是 Oracle 10g,则可以使用 DECODE 函数将行转为列:

                  If you are using Oracle 10g, you can use the DECODE function to pivot the rows into columns:

                  CREATE TABLE doc_tab (
                    loan_number VARCHAR2(20),
                    document_type VARCHAR2(20),
                    document_id VARCHAR2(20)
                  );
                  
                  INSERT INTO doc_tab VALUES('992452533663', 'Voters ID', 'XPD0355636');
                  INSERT INTO doc_tab VALUES('992452533663', 'Pan card', 'CHXPS5522D');
                  INSERT INTO doc_tab VALUES('992452533663', 'Drivers licence', 'DL-0420110141769');
                  
                  COMMIT;
                  
                  SELECT
                      loan_number,
                      MAX(DECODE(document_type, 'Voters ID', document_id)) AS voters_id,
                      MAX(DECODE(document_type, 'Pan card', document_id)) AS pan_card,
                      MAX(DECODE(document_type, 'Drivers licence', document_id)) AS drivers_licence
                    FROM
                      doc_tab
                  GROUP BY loan_number
                  ORDER BY loan_number;
                  

                  输出:

                  LOAN_NUMBER   VOTERS_ID            PAN_CARD             DRIVERS_LICENCE    
                  ------------- -------------------- -------------------- --------------------
                  992452533663  XPD0355636           CHXPS5522D           DL-0420110141769     

                  您可以使用 11g 中引入的 Oracle PIVOT 子句实现相同的目的:

                  You can achieve the same using Oracle PIVOT clause, introduced in 11g:

                  SELECT *
                    FROM doc_tab
                  PIVOT (
                    MAX(document_id) FOR document_type IN ('Voters ID','Pan card','Drivers licence')
                  );
                  

                  带有两种解决方案的 SQLFiddle 示例:SQLFiddle 示例

                  SQLFiddle example with both solutions: SQLFiddle example

                  在此处阅读有关透视的更多信息:透视蒂姆·霍尔的甲骨文

                  Read more about pivoting here: Pivot In Oracle by Tim Hall

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

                  上一篇:将数据数组作为输入参数传递给 Oracle 过程 下一篇:如果(从表中选择计数(列))>0 那么

                  相关文章

                  <tfoot id='qdvin'></tfoot>

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

                  • <bdo id='qdvin'></bdo><ul id='qdvin'></ul>

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

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