我有如下所示的查询
SELECT A.BUSINESS_UNIT
,A.ASSET_ID
,A.BOOK
,A.FISCAL_YEAR
,A.ACCOUNTING_PERIOD
,A.CATEGORY
,A.ACCOUNT_FA AS ACCOUNT
,A.CURRENCY_CD
,'COST_ADD' AS MOT
,A.COST_ADD AS AMOUNT
FROM SYSADM.PS_DEPR_RPT A
WHERE A.BOOK IN ('CORP-BASE','CORPORATE')
AND NOT (A.COST_ADD=0)
UNION
SELECT A1.BUSINESS_UNIT
,A1.ASSET_ID
,A1.BOOK
,A1.FISCAL_YEAR
,A1.ACCOUNTING_PERIOD
,A1.CATEGORY
,A1.ACCOUNT_AD AS ACCOUNT
,A1.CURRENCY_CD
,'ACCUM_DEPR_ADD' AS MOT
,A1.ACCUM_DEPR_ADD AS AMOUNT
FROM SYSADM.PS_DEPR_RPT A1
WHERE A1.BOOK IN ('CORP-BASE','CORPORATE')
AND NOT (A1.ACCUM_DEPR_ADD=0);
我想删除我在此使用的UNION以提高查询性能。由于运行查询需要大约120秒,我想让它更有效率。我使用了CASE。查询运行时间较短(4秒)但是,我的数据不匹配。请帮帮我。
PS:我使用CASE准备的查询如下。请提出是否需要更正。
select BUSINESS_UNIT
,ASSET_ID
,BOOK
,FISCAL_YEAR
,ACCOUNTING_PERIOD
,CATEGORY
,ACCOUNT
,MOT
,AMOUNT
FROM
(SELECT A.BUSINESS_UNIT
,A.ASSET_ID
,A.BOOK
,A.FISCAL_YEAR
,A.ACCOUNTING_PERIOD
,A.CATEGORY
,A.ACCOUNT_FA AS ACCOUNT
,A.CURRENCY_CD,
case when NOT (A.COST_ADD=0) then 'COST_ADD'
when NOT (A.ACCUM_DEPR_ADD=0) then 'ACCUM_DEPR_ADD'
else 'x'
END
as MOT,
case when NOT (A.COST_ADD=0) then A.COST_ADD
when NOT (A.ACCUM_DEPR_ADD=0) then A.ACCUM_DEPR_ADD
else 0
END
as AMOUNT
FROM SYSADM.PS_DEPR_RPT A
WHERE A.BOOK IN ('CORP-BASE','CORPORATE')) X
where mot <> 'x' and amount <> 0;
答案 0 :(得分:1)
你可以试试这个。
jetbrains
答案 1 :(得分:0)
SELECT
A.BUSINESS_UNIT
,A.ASSET_ID
,A.BOOK
,A.FISCAL_YEAR
,A.ACCOUNTING_PERIOD
,A.CATEGORY
,A.ACCOUNT_FA AS ACCOUNT
,A.CURRENCY_CD
,CASE WHEN A.COST_ADD <> 0 THEN 'COST_ADD' ELSE 'ACCUM_DEPR_ADD' END AS MOT
,CASE WHEN A.COST_ADD <> 0 THEN A.COST_ADD ELSE A.ACCUM_DEPR_ADD END AS AMOUNT
FROM SYSADM.PS_DEPR_RPT A
WHERE A.BOOK IN ('CORP-BASE','CORPORATE')
and (A.COST_ADD <> 0 or A.ACCUM_DEPR_ADD <> 0);