游标中的case语句不起作用,但select sql本身工作正常。请指出问题
/* cursor for summed txns fee amounts of merchants */
DECLARE sumCursor CURSOR FOR
(
SELECT feeTxn.toParticipantId, txn.cardType, SUM(IF(feeTxn.isCredit='t', feeTxn.amt, -feeTxn.amt)) amt, MAX(feeTxn.feeTxnId) maxId, MIN(feeTxn.feeTxnId) minId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND CASE
WHEN includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId) THEN feeTxn.txnDate <= dTxnDate
ELSE feeTxn.txnDate < dTxnDate
END
GROUP BY feeTxn.toParticipantId, txn.cardType
);
答案 0 :(得分:0)
异常使用CASE。你可以没有它,这样的事情: -
DECLARE sumCursor CURSOR FOR
(
SELECT feeTxn.toParticipantId, txn.cardType, SUM(IF(feeTxn.isCredit='t', feeTxn.amt, -feeTxn.amt)) amt, MAX(feeTxn.feeTxnId) maxId, MIN(feeTxn.feeTxnId) minId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND ((includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId) AND feeTxn.txnDate <= dTxnDate)
OR feeTxn.txnDate < dTxnDate )
GROUP BY feeTxn.toParticipantId, txn.cardType
);
在没有看到数据的情况下,我不确定以下内容是否会有所帮助,但考虑到您使用少量数据的函数并有效地对数据进行OR运算,如果您拆分数据,可能会帮助MySQL更有效地使用索引来提高性能查询多个联合在一起的查询。这样的事情: -
SELECT toParticipantId, cardType, SUM(amt) AS amt, MAX(feeTxnId) AS maxId, MIN(feeTxnId) AS minId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM (
SELECT feeTxn.toParticipantId, txn.cardType, feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId)
AND feeTxn.txnDate = dTxnDate
AND feeTxn.isCredit = 't'
UNION
SELECT feeTxn.toParticipantId, txn.cardType, feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND feeTxn.txnDate < dTxnDate
AND feeTxn.isCredit = 't'
UNION
SELECT feeTxn.toParticipantId, txn.cardType, -feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId)
AND feeTxn.txnDate = dTxnDate
AND feeTxn.isCredit != 't'
UNION
SELECT feeTxn.toParticipantId, txn.cardType, -feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND feeTxn.txnDate < dTxnDate
AND feeTxn.isCredit != 't'
} Sub1
GROUP BY toParticipantId, cardType