EmpSalary
表:
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
答案 0 :(得分:0)
实际上,我们需要您使用的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;