我需要像下面那样构造 table,动态使用 pivot 或 unpivot
I need to construct table like below, dynamically using pivot or unpivot
EmpSalary
table:
EmpCode BASIC HRA CONV
--------------------------
1 10000 500 300
2 10000 500 300
期望的输出:
SalaryCode SalaryDetails
EmpCode 1
BASIC 10000
HRA 500
CONV 300
Total 10800
EmpCode 2
BASIC 10000
HRA 500
CONV 300
Total 10800
实际上我们需要您正在使用的DBMS
。
包含 unpivot
子句的以下代码适用于 Sql-Server
或 Oracle
:
select SalaryCode, SalaryDetails
from
(select EmpCode, BASIC,HRA,CONV,
(BASIC+HRA+CONV) sub_Total
from EmpSalary
) p
unpivot
(SalaryDetails for SalaryCode in
(EmpCode,BASIC,HRA,CONV,sub_Total)
) unpvt;
EmpSalary
table:
EmpCode BASIC HRA CONV
--------------------------
1 10000 500 300
2 10000 500 300
期望的输出:
SalaryCode SalaryDetails
EmpCode 1
BASIC 10000
HRA 500
CONV 300
Total 10800
EmpCode 2
BASIC 10000
HRA 500
CONV 300
Total 10800
实际上我们需要您正在使用的DBMS
。
包含 unpivot
子句的以下代码适用于 Sql-Server
或 Oracle
:
select SalaryCode, SalaryDetails
from
(select EmpCode, BASIC,HRA,CONV,
(BASIC+HRA+CONV) sub_Total
from EmpSalary
) p
unpivot
(SalaryDetails for SalaryCode in
(EmpCode,BASIC,HRA,CONV,sub_Total)
) unpvt;