1

我想为表后面的列聚合多个 XMLELEMENT 并返回一个 xml 文件。

这是表格数据: 在此处输入图像描述

我想返回以下格式的 xml:

<employee>
<id>FMCSC00015</id>
<year>2016</year>
<month>1</month>
<head_name>BASIC PAY</head_name>
<amount>35600</amount>
<head_name>BANK LOAN-4</head_name>
<amount>23490</amount>
<head_name>RESEARCH ALLOWANCE</head_name>
<amount>1500</amount>
<head_name>MOTOR GARAGE</head_name>
<amount>500.5</amount>
<head_name>CLUB</head_name>
<amount>207</amount>
.........so on            
<employee>

但是我在汇总 2 列 [head_name 和 amount] 时遇到了困难。这是我的甲骨文代码:

 select xmlElement(  "employee",
                      xmlelement("id", e.PAYMSTR_EMPID),
                      xmlelement("year", e.PAYMSTR_SALYR),
                      xmlelement("month", e.PAYMSTR_SALMT),
                      XMLAGG(
                        XMLELEMENT(" ",
                        XMLELEMENT("head_name", e.PAYMSTR_SALHDNM ),
                        XMLELEMENT("amount", e.PAYMSTR_AMOUNT )  ) 
                      )
                  ) as result 
                  from TBL_PAYROLL_MASTER_FILE e                
                  where e.PAYMSTR_EMPID = 'FMCSC00015'
                  group by e.PAYMSTR_EMPID,e.PAYMSTR_SALYR, e.PAYMSTR_SALMT;

正如你所看到的,因为我在 XMLAGG 的第一个 XMLELEMENT 中使用了空格,它仍然会创建具有以下输出 xml 的空标签:

<employee>
    <id>FMCSC00015</id>
    <year>2016</year>
    <month>1</month>
    <>
    <head_name>BASIC PAY</head_name>
    <amount>35600</amount>
    </>
    <>
    <head_name>BANK LOAN-4</head_name>
    <amount>23490</amount>
    </>
    <>
    <head_name>RESEARCH ALLOWANCE</head_name>
    <amount>1500</amount>
    </>
    <>    
    <head_name>MOTOR GARAGE</head_name>
    <amount>500.5</amount>
    </>
    <>
    <head_name>CLUB</head_name>
    <amount>207</amount>
    .........so on            
    <employee>

我怎样才能避免这些额外的空标签并获得我适当的 xml 格式。谢谢

4

1 回答 1

1

我认为您应该专注于XMLFOREST,这很成功。

此查询提供所需的 XML。

select   xmlElement(  "employee",
              xmlelement("id", e.PAYMSTR_EMPID),
              xmlelement("year", e.PAYMSTR_SALYR),
              xmlelement("month", e.PAYMSTR_SALMT),
              (select   XMLAGG(  
                 xmlforest(PAYMSTR_SALHDNM, PAYMSTR_AMOUNT  ) )
               from my_tab s where  s.PAYMSTR_EMPID = e.PAYMSTR_EMPID and 
                  s.PAYMSTR_SALYR = e.PAYMSTR_SALYR and s.PAYMSTR_SALMT = e.PAYMSTR_SALMT)      
                      )
from  
   ( select distinct PAYMSTR_EMPID, PAYMSTR_SALYR, PAYMSTR_SALMT 
     from  my_tab where  PAYMSTR_EMPID = 'FMCSC00015' ) e;

请注意,因为您的数据是非规范化的,所以我首先选择不同的员工、年和月,然后在子查询中选择所有员工行(获取年和月)。

<employee>
  <id>FMCSC00015</id>
  <year>2016</year>
  <month>1</month>
  <PAYMSTR_SALHDNM>BASIC PAY</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>35600</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>BANK LOAN-4</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>23490</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>RESEARCH ALLOWANCE</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>1500</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>MOTOR GARAGE</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>500,5</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>CLUB</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>207</PAYMSTR_AMOUNT>
</employee>

如果您的员工有更多记录(不同年份和月份,您会为每个记录获得一个 XML 行

<employee>
  <id>FMCSC00015</id>
  <year>2015</year>
  <month>1</month>
  <PAYMSTR_SALHDNM>CLUB</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>207</PAYMSTR_AMOUNT>
</employee>

我的测试数据

create table my_tab as 
select 'FMCSC00015' PAYMSTR_EMPID, 2016 PAYMSTR_SALYR, 1 PAYMSTR_SALMT, 'BASIC PAY' PAYMSTR_SALHDNM, 35600 PAYMSTR_AMOUNT from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'BANK LOAN-4' head_name, 23490 amount from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'RESEARCH ALLOWANCE' head_name, 1500 amount from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'MOTOR GARAGE' head_name, 500.5 amount from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'CLUB' head_name, 207 amount from dual union all
select 'FMCSC00015' id, 2015 year, 1 month, 'CLUB' head_name, 207 amount from dual union all
select 'FMCSC00016' id, 2016 year, 1 month, 'CLUB' head_name, 207 amount from dual;
于 2016-01-31T17:59:55.257 回答