1

游标中的 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
  );
4

1 回答 1

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
);

在没有看到数据的情况下,我不确定以下内容是否有帮助,但鉴于您对少量数据使用函数并有效地对数据进行 ORing,如果您将查询拆分为,它可能有助于 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
于 2013-05-22T15:48:38.887 回答