oracle从行移到列

时间:2019-05-16 17:08:49

标签: oracle oracle10g

SELECT 'orf1' AS orf_ref,
  1           AS seq_num ,
  'PO'        AS column_name,
  'po-row-1'  AS colume_value
FROM dual
UNION ALL
SELECT 'orf1' AS orf_ref,
  2           AS seq_num ,
  'PO'        AS column_name,
  'po-row-2'  AS colume_value
FROM dual
UNION ALL
SELECT 'orf1'   AS orf_ref,
  1             AS seq_num ,
  'Title'       AS column_name,
  'Title-row-1' AS colume_value
FROM dual
UNION ALL
SELECT 'orf1'   AS orf_ref,
  2             AS seq_num ,
  'Title'       AS column_name,
  'Title-row-2' AS colume_value
FROM dual
UNION ALL
SELECT 'orf2' AS orf_ref,
  1           AS seq_num ,
  'PO'        AS column_name,
  'po-row-3'  AS colume_value
FROM dual
UNION ALL
SELECT 'orf2' AS orf_ref,
  2           AS seq_num ,
  'PO'        AS column_name,
  'po-row-4'  AS colume_value
FROM dual
UNION ALL
SELECT 'orf2'   AS orf_ref,
  1             AS seq_num ,
  'Title'       AS column_name,
  'Title-row-3' AS colume_value
FROM dual
UNION ALL
SELECT 'orf2'   AS orf_ref,
  2             AS seq_num ,
  'Title'       AS column_name,
  'Title-row-4' AS colume_value
FROM dual;


上面的查询将导致:

<html>
<table>
<tr><td>ORF_REF</td><td>SEQ_NUM</td><td>COLUMN_NAME</td><td>COLUME_VALUE</td><tr>
<tr><td>orf1</td><td>1</td><td>PO</td><td>po-row-1<td></tr>
<tr><td>orf1</td><td>2</td><td>PO</td><td>po-row-2</td></tr>
<tr><td>orf1</td><td>1</td><td>Title</td><td>Title-row-1</td></tr>
<tr><td>orf1</td><td>2</td><td>Title</td><td>Title-row-2</td></tr>
<tr><td>orf2</td><td>1</td><td>PO</td><td>po-row-3</td></tr>
<tr><td>orf2</td><td>2</td><td>PO</td><td>po-row-4</td></tr>
<tr><td>orf2</td><td>1</td><td>Title</td><td>Title-row-3</td></tr>
<tr><td>orf2</td><td>2</td><td>Title</td><td>Title-row-4</td></tr>
</table>
<html>


预期结果:

<html>
    <table>
    <tr><td>ORF</td><td>PO</td><td>TITLE</td></tr>
    <tr><td>orf1</td><td>po-row-1</td><td>Title-row-1</td></tr>
    <tr><td>orf1</td><td>po-row-2</td><td>Title-row-2</td></tr>
    <tr><td>orf2</td><td>po-row-3</td><td>Title-row-3</td></tr>
    <tr><td>orf2</td><td>po-row-4</td><td>Title-row-4</td></tr>
    </table>
    </html>



请建议在oracle10g中使用sql以获得预期的结果

0 个答案:

没有答案