SELECT acctVw.acct_cd AS acctCd,
acctVw.full_name AS fullName,
acctVw.lv1_acct_nm AS lv1AcctNm,
DATE_FORMAT( DATE_ADD(vh.vchr_date,
INTERVAL 8 HOUR),
'%Y-%m-%d' ) AS vchrDate, CONCAT( '记', '-', (
CASE
WHEN LENGTH(vh.VCHR_NO) <= 4 THEN
CONCAT( SUBSTRING( '000', 1, 4 - LENGTH(vh.VCHR_NO) ), vh.VCHR_NO )
ELSE vh.VCHR_NO
END ) ) AS vchrNo, vh.perd_cd AS perdCd, vw.VCHR_ABSTR AS vchrAbstr, vw.DEBIT_FRGN_AMT AS debitOrgi, vw.CREDIT_FRGN_AMT AS creditOrgi, vw.DEBIT_QTY AS debitQty, vw.DEBIT_AMT AS debitAmt, vh.ID AS vchrId, CONCAT( acctVw.acct_cd, ' ', acctVw.full_name, IF ( acctVw.NODE_TYPE_CD <> '1135.150', CONCAT( '', IF ( vw.DEALER_CD <> '', CONCAT( '-', vw.DEALER_CD, ' ', vw.DEALER_NM ), '' ), IF ( vw.DEPT_CD <> '', CONCAT( '-', vw.DEPT_CD, ' ', vw.DEPT_NM ), '' ), IF ( vw.EMP_CD <> '', CONCAT( '-', vw.EMP_CD, ' ', vw.EMP_NM ), '' ), IF ( vw.ITEM_CD <> '', CONCAT( '-', vw.ITEM_CD, ' ', vw.ITEM_NM ), '' ), IF ( vw.INVEN_CD <> '', CONCAT( '-', vw.INVEN_CD, ' ', vw.INVEN_NM, ' 单位:', vw.mesr_unit_nm ), '' ),
IF ( vw.ASSET_CD <> '', CONCAT( '-',
vw.ASSET_CD,
' ',
vw.ASSET_NM ),
'' ) ), '' ) ) AS dispCdAndNm, CONCAT( acctVw.acct_cd, ' ', acctVw.acct_nm, IF ( acctVw.NODE_TYPE_CD <> '1135.150', CONCAT( '', IF ( vw.DEALER_CD <> '', CONCAT('-', vw.DEALER_CD), '' ), IF ( vw.DEPT_CD <> '', CONCAT('-', vw.DEPT_CD), '' ), IF ( vw.EMP_CD <> '', CONCAT('-', vw.EMP_CD), '' ), IF ( vw.ITEM_CD <> '', CONCAT('-', ITEM_CD), '' ), IF ( vw.INVEN_CD <> '', CONCAT('-', vw.INVEN_CD), '' ),
IF ( vw.ASSET_CD <> '', CONCAT('-',
vw.ASSET_CD),
'' ) ), '' ) ) AS dispNm, vw.exch_Rate AS exchRate, vw.LINE_NUM AS lineNum, acctVw.APRV_CURR_FLG AS aprvCurrFlg, acctVw.APRV_AMT_FLG AS aprvAmtFlg, acctVw.NODE_TYPE_CD AS nodeTypeCd, (
CASE
WHEN vw.DEBIT_QTY <> 0 THEN
ROUND( vw.DEBIT_AMT / vw.DEBIT_QTY, 5 )
ELSE 0
END ) debitPrice, vw.CREDIT_QTY AS creditQty, vw.CREDIT_AMT AS creditAmt, (
CASE
WHEN vw.CREDIT_QTY <> 0 THEN
ROUND( vw.CREDIT_AMT / vw.CREDIT_QTY, 5 )
ELSE 0
END ) creditPrice
FROM FI_VCHR vh
LEFT JOIN VW_FI_VCHR_DTL vw
ON vh.CORP_ID = vw.CORP_ID
AND vh.ID = vw.VCHR_ID
LEFT JOIN FI_ACCT_HIST_MAP ahm
ON vw.acct_id = ahm.prev_id
AND vw.CORP_ID = ahm.CORP_ID
LEFT JOIN vw_fi_acct_fnm acctVw
ON vh.CORP_ID = acctVw.CORP_ID
AND ahm.corp_id = acctVw.corp_Id
AND ahm.curr_Cd LIKE CONCAT(acctVw.acct_cd, '%')
AND ahm.curr_year = acctVw.hist_perd
WHERE 1 = 1
AND ahm.curr_year = LEFT ('202001', 4)
AND ahm.curr_cd >= '1001'
AND ( ahm.curr_cd <= '5801'
OR ahm.curr_cd LIKE CONCAT('5801', '%') )
AND vw.CORP_ID = '4807b90ad7744f2bb1021f465213432d'
AND vh.perd_cd >= '202001'
AND vh.perd_cd <= '202001'
语句太复杂了,建议引入视图,拆分成几个语句好一点。