第一个是max(decode( 两个函数一起应用 的行转列(oracle)
第二个是 case when的 行转列(mysql oracle 都可以 (好像))
两者需要注意的
这里面 的group by 参数 是和函数一样的数据 不能有不一致的数据 比如说 主键id 主键ID是这几行数据都不一样的 加上去之后 就会把原来几条都查出来
SELECT
max(decode(hs.PAY_TAB_CODE, '235', hs.PAY_TAB_NAME)) payrollTax,max(decode(hs.PAY_TAB_CODE, '231', hs.PAY_TAB_NAME)) taxableWages
,max(decode(hs.PAY_TAB_CODE, '242', hs.PAY_TAB_NAME)) paidWages,max(decode(hs.PAY_TAB_CODE, '239', hs.PAY_TAB_NAME)) payable
,max(decode(hs.PAY_TAB_CODE, '235', hs.T00HS027ID)) payrollTaxId,max(decode(hs.PAY_TAB_CODE, '231', hs.T00HS027ID)) taxableWagesId
,max(decode(hs.PAY_TAB_CODE, '242', hs.T00HS027ID)) paidWagesId,max(decode(hs.PAY_TAB_CODE, '239', hs.T00HS027ID)) payableId
,hs.EMPID,hs."MONTH",hs."YEAR",hs.CREATE_DATE createDate,hs.CREATOR,hm2.EMPNO AS CREATORNO,hs.COMPID,HM.EMPNO,HM. NAME AS EmpName
FROM T00HS027 hs , T00HM001 hm , T00HM001 hm2WHEREhs.EMPID = hm.EMPIDAND hs.T00HM001ID = hm2.EMPIDAND hs."YEAR" = 2023AND hs."MONTH" = 2AND hs.COMPID = '2eec30c2dcb34a1e97c570d34ff900f4'
AND
( hs.PAY_TAB_CODE = '235' OR hs.PAY_TAB_CODE = '242' OR hs.PAY_TAB_CODE = '231' OR hs.PAY_TAB_CODE = '239' )GROUP BY
hs.EMPID,hs."MONTH",hs."YEAR",hs.CREATE_DATE ,hs.CREATOR,hm2.EMPNO ,hs.COMPID,HM.EMPNO,HM.NAME
ORDER BY hs."YEAR",hs.MONTH
SELECThs.empId,
hs."MONTH",hs."YEAR",hs.CREATE_DATE createDate,hs.CREATOR,hm2.EMPNO AS CREATORNO,hs.COMPID,HM.EMPNO,HM. NAME AS EmpName,MAX (CASEWHEN hs.PAY_TAB_CODE = '235' THENhs.PAY_TAB_NAMEEND) payrollTax,MAX (CASEWHEN hs.PAY_TAB_CODE = '231' THENhs.PAY_TAB_NAMEEND) taxableWages,
MAX (CASEWHEN hs.PAY_TAB_CODE = '242' THENhs.PAY_TAB_NAMEEND) paidWages,
MAX (CASEWHEN hs.PAY_TAB_CODE = '239' THENhs.PAY_TAB_NAMEEND) payable,
MAX (CASEWHEN hs.PAY_TAB_CODE = '235' THENhs.T00HS027IDEND) payrollTaxId,
MAX (CASEWHEN hs.PAY_TAB_CODE = '231' THENhs.T00HS027IDEND) taxableWagesId,MAX (CASEWHEN hs.PAY_TAB_CODE = '242' THENhs.T00HS027IDEND) paidWagesId,MAX (CASEWHEN hs.PAY_TAB_CODE = '239' THENhs.T00HS027IDEND) payableIdFROMT00HS027 hs,T00HM001 hm,T00HM001 hm2WHEREhs.EMPID = hm.EMPIDAND hs.T00HM001ID = hm2.EMPIDAND hs."YEAR" = 2023AND hs."MONTH" = 2AND hs.COMPID = '2eec30c2dcb34a1e97c570d34ff900f4'
GROUP BYhs.empId,hs."YEAR"
,hs."MONTH",hs.CREATE_DATE ,hs.CREATOR,hm2.EMPNO ,
hs.COMPID,HM.EMPNO,HM. NAME