PostgreSQL的exists改关联查询
一、exists改关联查询
二、with里面的where条件放到最终查询join的on里面
三、示例SQL
WITH t AS (
select t
.FUND_TRANSFER_APPLY_ID,
t.proc_inst_id AS procInstId,
(
select
fmt.message
from
fnd.tpl_fd_message_t fmt
where
fmt.app_name = 'etreasury'
and fmt.message_key = t.workflow_status
and fmt.language = 'en_US'
limit 1
) as status,
coalesce (
(
select
( select u.lname from fnd.tpl_user_t u where u.user_id = ta.assignee_ :: numeric )
from
ewf.act_ru_task AS ta
where
ta.proc_inst_id_ = t.receive_procinst_id
),
(
SELECT
string_agg ( ( SELECT u.lname FROM fnd.tpl_user_t u WHERE u.user_id = twhi.user_id_ ), ',' )
FROM
ewf.tpl_wf_hi_identitylink_t AS twhi,
ewf.act_hi_taskinst AS art
WHERE
twhi.task_id_ = art.id_
AND art.proc_inst_id_ :: VARCHAR = t.receive_procinst_id
AND ( art.delete_reason_ != 'completed' OR art.delete_reason_ IS NULL )
AND t.receive_wf_status = art.description_
AND art.description_ != 'etreasury.deal.receivefund.flow.completed'
)
) AS currentHandler,
t.deal_no as dealNumber,
t.ticket_no as ticketNumber,
( select im.instrument_name from ets.ets_bas_instrument_t im where im.bas_instrument_id = t.bas_instrument_id limit 1 ) as instrument,
( select currency_code from ets.ets_mdm_rm_currency_info_t ct where ct.currencies_id = t.payment_ccy_id limit 1 ) as paymentCurrency,
round( t.payment_amount, 2 ) as paymentAmount,
to_char( t.payment_date, 'yyyy-MM-dd' ) as paymentDate,
round( t.equivalent_usd_amount, 2 ) as equivalentUSDAmount,
CASE
WHEN t.urgent_flag = 1 THEN
'Yes'
WHEN t.urgent_flag = 0 THEN
'No'
END AS urgent,
round( t.wht_amount, 2 ) as wHTAmount,
( select et.management_entity_en_name from ets.ets_bas_management_entity_t et where et.bas_management_entity_id = t.back_to_back_subsidiary_id limit 1 ) as b2B3rdParty,
t.back_to_back_deal_no as b2BDealNo,
( select et.management_entity_en_name from ets.ets_bas_management_entity_t et where et.bas_management_entity_id = t.source_mgt_entity_id limit 1 ) as sourceEntity,
( select ct.company_code from ets.ets_company_t ct where ct.company_id = t.source_company_id limit 1 ) as sourceCompanyCode,
( select bt.bank_account_no from ets_bbam.finbas_bank_account_t bt where bt.bank_account_id = t.source_bank_account_id limit 1 ) as sourceBankAccountNo,
( select currency_code from ets.ets_mdm_rm_currency_info_t ct where ct.currencies_id = t.source_acc_ccy_id limit 1 ) as sourceAccountCurrency,
round( t.source_amount, 2 ) as sourceAmount,
(
select
bnt.account_nature
from
(
SELECT
n.bas_account_nature_id,
n.account_nature_name,
CASE
WHEN coalesce ( n.parent_id :: VARCHAR, '' ) = '' THEN
n.account_nature_name ELSE n.parent_name
END AS account_nature
FROM
(
SELECT t
.bas_account_nature_id,
t.parent_id,
( SELECT p.account_nature_name FROM ets_bbam.ets_bas_account_nature_t p WHERE p.bas_account_nature_id = t.parent_id ) parent_name,
t.account_nature_name,
t.account_nature_name_cn,
t.status,
t.account_nature_type
FROM
ets_bbam.ets_bas_account_nature_t t
) AS n
) bnt
where
bnt.bas_account_nature_id = t.source_account_nature
) as sourceAccountNature,
t.source_bank_name as sourceBankName,
(
SELECT
fbt.cparty_short_name
FROM
ets_bbam.finbas_bank_account_t bat,
ets_bbam.finbas_bankacc_currency_t bct,
ets_bbam.finbas_cparty_t fbt
WHERE
bct.owner_id = bat.bank_account_id
AND bat.bank_id = fbt.cparty_id
AND bct.currency_id = t.source_acc_ccy_id
AND bat.bank_account_id = t.source_bank_account_id
AND bct.owner_type = 1
LIMIT 1
) as sourceBankShortName,
t.source_bank_branch_name as sourceBankBranchName,
t.source_convert_rate as sourceFXconvertRate,
( select et.management_entity_en_name from ets.ets_bas_management_entity_t et where et.bas_management_entity_id = t.destination_mgt_entity_id limit 1 ) as destinationEntity,
( select ct.company_code from ets.ets_company_t ct where ct.company_id = t.destination_company_id limit 1 ) as destinationCompanyCode,
( select bt.bank_account_no from ets_bbam.finbas_bank_account_t bt where bt.bank_account_id = t.destination_bank_account_id limit 1 ) as destinationBankAccountNo,
( select currency_code from ets.ets_mdm_rm_currency_info_t ct where ct.currencies_id = t.destination_acc_ccy_id limit 1 ) destinationAccountCurrency,
round( t.destination_amount, 2 ) as destinationAmount,
(
select
bnt.account_nature
from
(
SELECT
n.bas_account_nature_id,
n.account_nature_name,
CASE
WHEN coalesce ( n.parent_id :: VARCHAR, '' ) = '' THEN
n.account_nature_name ELSE n.parent_name
END AS account_nature
FROM
(
SELECT t
.bas_account_nature_id,
t.parent_id,
( SELECT p.account_nature_name FROM ets_bbam.ets_bas_account_nature_t p WHERE p.bas_account_nature_id = t.parent_id ) parent_name,
t.account_nature_name,
t.account_nature_name_cn,
t.status,
t.account_nature_type
FROM
ets_bbam.ets_bas_account_nature_t t
) AS n
) bnt
where
bnt.bas_account_nature_id = t.destination_account_nature
) as destinationAccountNature,
t.destination_bank_name as destinationBankName,
(
SELECT
fbt.cparty_short_name
FROM
ets_bbam.finbas_bank_account_t bat,
ets_bbam.finbas_bankacc_currency_t bct,
ets_bbam.finbas_cparty_t fbt
WHERE
bct.owner_id = bat.bank_account_id
AND bat.bank_id = fbt.cparty_id
AND bct.currency_id = t.destination_acc_ccy_id
AND bat.bank_account_id = t.destination_bank_account_id
AND bct.owner_type = 1
LIMIT 1
) as destinationBankShortName,
t.destination_bank_branch_name as destinationBankBranchName,
t.destination_convert_rate as destinationFXconvertRate,
( select u.lname from fnd.tpl_user_t u where u.user_id = t.created_by limit 1 ) as dealer,
to_char( t.deal_date, 'yyyy-MM-dd' ) as dealDate,
to_char( t.creation_date, 'yyyy-MM-dd hh24:mi:ss' ) as createTime,
t.payment_method as paymentMethod,
t.document_no as documentNo,
t.deal_date,
CASE
WHEN t.fund_transfer_reason = 0 THEN
'Drawdown'
WHEN t.fund_transfer_reason = 1 THEN
'Repayment'
WHEN t.fund_transfer_reason = 2 THEN
''
END AS fundTransferReason,
CASE
WHEN t.print_Status = '0' THEN
'Unprinted'
WHEN t.print_Status = '1' THEN
'Printed' ELSE''
END AS printStatus,
t.reprint_reason reprintReason
,t.source_mgt_entity_id AS source_mgt_entity_id
from
ets.ets_deal_fund_transfer_apply_t t,
ets.ets_bas_management_entity_t me
,(SELECT distinct lit.item_name
FROM fnd.tpl_lookup_item_t lit
WHERE 1=1
AND lit.classify_code = 'RECEIVE_I18NSTATUS'
AND lit.app_name = 'etreasury'
) lit
where
me.bas_management_entity_id = t.source_mgt_entity_id
and t.receive_wf_status = lit.item_name
and t.source_id != 2
and t.channel_delete_flag = 0
)
,ttmp AS (
SELECT
TA.*
FROM
(
SELECT
TWL.ASSIGNEE,
(
SELECT coalesce
( MAX ( TFM.MESSAGE ), TWL.TASK_DEF_NAME )
FROM
fnd.TPL_FD_MESSAGE_T TFM
WHERE
TFM.MESSAGE_KEY = TWL.TASK_DEF_NAME
AND TFM.LANGUAGE = 'en_US'
AND TFM.APP_NAME = 'etreasury'
) TASK_NAME,
PROCESS_INSTANCE_ID AS PROC_INST_ID_,
TWL.CREATION_DATE,
ROW_NUMBER ( ) OVER ( PARTITION BY TWL.PROCESS_INSTANCE_ID, TWL.TASK_DEF_NAME ORDER BY TWL.CREATION_DATE DESC ) RN
FROM
EWF.TPL_WF_LOG_T TWL
WHERE
TWL.TYPE = 0
AND TWL.PROCESS_INSTANCE_ID IN ( SELECT t.procInstId FROM t )
) TA
WHERE
RN = 1
)
, ttmp2 AS (
select
ttmp.PROC_INST_ID_,
CASE
WHEN ttmp.TASK_NAME = 'Supervisor' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as supervisor,
CASE
WHEN ttmp.TASK_NAME = 'Approver' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as Approver,
CASE
WHEN ttmp.TASK_NAME = 'Accountant' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END AS ACCOUNTANT,
CASE
WHEN ttmp.TASK_NAME = 'Accountant Reviewer' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as accountantReviewer,
CASE
WHEN ttmp.TASK_NAME = 'Actual Pay Cashier' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as actualPayCashier,
CASE
WHEN ttmp.TASK_NAME = 'Local Cashier' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as localCashier,
CASE
WHEN ttmp.TASK_NAME = 'eBanking Group C' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as eBankingGroupC,
CASE
WHEN ttmp.TASK_NAME = 'eBanking Group B' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as eBankingGroupB,
CASE
WHEN ttmp.TASK_NAME = 'eBanking Group A' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as eBankingGroupA,
CASE
WHEN ttmp.TASK_NAME = 'Bill Group B' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END AS billGroupB,
CASE
WHEN ttmp.TASK_NAME = 'Bill Group A' THEN
( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE''
END as billGroupA
from
ttmp
)
,t2 AS (
SELECT
proc_inst_id_,
( regexp_match ( approver :: text, '[^,]+' ) ) [ 1 ] AS approver,
( SELECT ( regexp_matches( approver :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS approverTime,
( regexp_match ( accountant :: text, '[^,]+' ) ) [ 1 ] AS accountant,
( SELECT ( regexp_matches( accountant :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS accountantSignTime,
( regexp_match ( accountantReviewer :: text, '[^,]+' ) ) [ 1 ] AS reviewer,
( SELECT ( regexp_matches( accountantReviewer :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS reviewerSignTime,
( regexp_match ( actualPayCashier :: text, '[^,]+' ) ) [ 1 ] AS cashier,
( SELECT ( regexp_matches( actualPayCashier :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS cashierSignTime,
( regexp_match ( localCashier :: text, '[^,]+' ) ) [ 1 ] AS localCashier,
( SELECT ( regexp_matches( localCashier :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS localCashierSignTime,
( regexp_match ( eBankingGroupC :: text, '[^,]+' ) ) [ 1 ] AS eBankingGroupC,
( SELECT ( regexp_matches( eBankingGroupC :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS eBankingGroupCSignTime,
( regexp_match ( eBankingGroupB :: text, '[^,]+' ) ) [ 1 ] AS eBankingGroupB,
( SELECT ( regexp_matches( eBankingGroupB :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS eBankingGroupBSignTime,
( regexp_match ( eBankingGroupA :: text, '[^,]+' ) ) [ 1 ] AS eBankingGroupA,
( SELECT ( regexp_matches( eBankingGroupA :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS ebankingGroupASignTime,
( regexp_match ( billGroupB :: text, '[^,]+' ) ) [ 1 ] AS billGroupB,
( SELECT ( regexp_matches( billGroupB :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS billGroupBSignTime,
( regexp_match ( billGroupA :: text, '[^,]+' ) ) [ 1 ] AS billGroupA,
( SELECT ( regexp_matches( billGroupA :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS billGroupASignTime
FROM
(
SELECT
TA.*
FROM
(
SELECT
PROC_INST_ID_,
MAX ( supervisor ) AS supervisor,
MAX ( Approver ) AS Approver,
MAX ( ACCOUNTANT ) AS ACCOUNTANT,
MAX ( accountantReviewer ) AS accountantReviewer,
MAX ( actualPayCashier ) AS actualPayCashier,
MAX ( localCashier ) AS localCashier,
MAX ( eBankingGroupC ) AS eBankingGroupC,
MAX ( eBankingGroupB ) AS eBankingGroupB,
MAX ( eBankingGroupA ) AS eBankingGroupA,
MAX ( billGroupB ) AS billGroupB,
MAX ( billGroupA ) AS billGroupA
FROM
ttmp2
group by
PROC_INST_ID_
) TA
) tt
)
SELECT t
.FUND_TRANSFER_APPLY_ID,
t.procInstId,
t.status,
t.currentHandler,
t.dealNumber,
t.ticketNumber,
t.instrument,
t.paymentCurrency,
t.paymentAmount,
t.paymentDate,
t.equivalentUSDAmount,
t.urgent,
t.wHTAmount,
t.b2B3rdParty,
t.b2BDealNo,
t.sourceEntity,
t.sourceCompanyCode,
t.sourceBankAccountNo,
t.sourceAccountCurrency,
t.sourceAmount,
t.sourceAccountNature,
t.sourceBankName,
t.sourceBankShortName,
t.sourceBankBranchName,
t.sourceFXconvertRate,
t.destinationEntity,
t.destinationCompanyCode,
t.destinationBankAccountNo,
t.destinationAccountCurrency,
t.destinationAmount,
t.destinationAccountNature,
t.destinationBankName,
t.destinationBankShortName,
t.destinationBankBranchName,
t.destinationFXconvertRate,
t.dealer,
t.dealDate,
t.createTime,
t2.approver,
t2.approverTime,
t2.accountant,
t2.accountantSignTime,
t2.reviewer,
t2.reviewerSignTime,
t2.cashier,
t.paymentMethod,
t.documentNo,
t2.cashierSignTime,
t2.localCashier,
t2.localCashierSignTime,
t2.eBankingGroupC,
t2.eBankingGroupCSignTime,
t2.eBankingGroupB,
t2.eBankingGroupBSignTime,
t2.eBankingGroupA,
t2.ebankingGroupASignTime,
t2.billGroupB,
t2.billGroupBSignTime,
t2.billGroupA,
t2.billGroupASignTime,
t.fundTransferReason,
t.printStatus,
t.reprintReason
FROM t inner join t2
on t.procInstId = t2.proc_inst_id_
and t.source_mgt_entity_id not in (
'229',
'247',
'351',
'230',
'506',
'112',
'224',
'231',
'233',
'221',
'1845',
'294',
'2145',
'204'
)
order by
t.dealDate desc,
t.FUND_TRANSFER_APPLY_ID desc
四、PostgreSQL分页,查出结果后,用with当成临时表,再进行limit 15 offset 0。直接搞得话会很卡。