case语句时oracle过程游标查询

时间:2014-08-12 07:46:36

标签: oracle stored-procedures case-when cursors

CURSOR BULKUPDATE IS 
    SELECT SUM(B.ACCOUNT_BALANCE) AS ACCOUNT_BALANCE,C.CIF AS CIF_ID FROM _ACCOUNTS_STAGING2 B JOIN  _RELATION_STAGING2 C
    ON B.ACCOUNT_IDENTIFICATION_NUMBER = C.ACCOUNT_IDENTIFICATION_NUMBER AND B.SOURCEID=C.SOURCEID JOIN _CUSTOMER_STAGING2 A ON A.CIF=C.CIF AND A.SOURCEID=C.SOURCEID WHERE  C.ROLE_ON_ACCOUNT IN
    (Select Rollonaccount From _Roleaccount_Master Where Aggregatebalance='Y')
    And upper(B.Scheme_Type) In (Select Scheme_Type From _Schema_Type_Master Where 
    Depository_Account = 'Y') Group By C.Cif;
    Rec_Bulkupdate Bulkupdate%Rowtype;

我正在使用此查询根据不同的cif和source对帐户余额进行汇总。问题是我想在_Schema_Type_Master的基础上计算四种不同类型的和。例如,我想现在检查current_account ='Y'而不是Depository_Account ='Y'

_ACCOUNTS_STAGING2 B JOIN  _RELATION_STAGING2 C
    ON B.ACCOUNT_IDENTIFICATION_NUMBER = C.ACCOUNT_IDENTIFICATION_NUMBER AND B.SOURCEID=C.SOURCEID JOIN _CUSTOMER_STAGING2 A ON A.CIF=C.CIF AND A.SOURCEID=C.SOURCEID WHERE  C.ROLE_ON_ACCOUNT IN
    (Select Rollonaccount From _Roleaccount_Master Where Aggregatebalance='Y')
    And upper(B.Scheme_Type) In (Select Scheme_Type From _Schema_Type_Master Where 
    current_account='Y') Group By C.Cif;
    Rec_Bulkupdate Bulkupdate%Rowtype;

有什么办法,或者我需要为此编写四种不同的游标吗?

1 个答案:

答案 0 :(得分:0)

您可以删除dipository_account='Y'current_account='Y'并在select as -

中使用大小写
SELECT SUM(CASE WHEN Depository_Account = 'Y' THEN B.ACCOUNT_BALANCE ELSE 0 END) AS DIPOSITORY_ACCOUNT_BALANCE, 
       SUM(CASE WHEN current_account = 'Y' THEN B.ACCOUNT_BALANCE ELSE 0 END) AS CURRENT_ACCOUNT_BALANCE

然后是你的其余代码。您将获得两个不同的列,用于存储帐户和当前帐户的总和。

如果需要过滤dipository_account='Y'current_account='Y',请在条件为或者运算符的情况下使用它们:

AND (dipository_account='Y' or current_account='Y')