如何使用 XMLAGG 聚合多个 XMLELEMENT
How to aggregate multiple XMLELEMENT using XMLAGG
我想为 table 后面的列和 return 一个 xml 文件聚合多个 XMLELEMENT。
这是 table 数据:
我想要 return 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 和数量] 时遇到困难。
这是我的预言机代码:
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 中使用了白色 space,它仍然会创建 空标签,输出如下 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 format.Thanks
我认为你应该专注于 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;
请注意,因为您的数据是非规范化的,所以我首先 select 区分员工、年份和月份,然后在子查询中所有员工行(获取年份和月份)。
<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;
我想为 table 后面的列和 return 一个 xml 文件聚合多个 XMLELEMENT。
这是 table 数据:
我想要 return 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 和数量] 时遇到困难。 这是我的预言机代码:
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 中使用了白色 space,它仍然会创建 空标签,输出如下 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 format.Thanks
我认为你应该专注于 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;
请注意,因为您的数据是非规范化的,所以我首先 select 区分员工、年份和月份,然后在子查询中所有员工行(获取年份和月份)。
<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;