分类: Oracle
2013-01-16 19:59:38
decode 函数中如果结果又聚合函数,在group by语句中需要将decode判断的值或者项也加进去
例如:
SELECT A.C_FUNDCODE,
A.D_REGISTERDATE,
DECODE(A.C_FLAG,
'1',
NVL(A.F_TOTALPROFIT, 0),
'0',
SUM(ROUND(NVL(C.F_REALSHARES, 0) * NVL(A.F_UNITPROFIT, 0), 2))) BUDGETBALANCE,
SUM(NVL(C.F_REALSHARES, 0)) TOTALSHARES,
NVL(A.F_UNITPROFIT, 0) UNITPROFIT,
NVL(A.F_TOTALPROFIT, 0) TOTALPROFIT
FROM TPROFITSCHEMA A, TSTATICSHARES C
WHERE A.D_REGISTERDATE = TO_DATE(:GConfirmDate, 'YYYYMMDD')
AND A.C_FUNDCODE = C.C_FUNDCODE
AND INSTR(NVL(:sFundcode, A.C_FUNDCODE), A.C_FUNDCODE) > 0;
这条sql语句中decode函数中就存在有聚合函数sum,那么它的group语句如何书写呢?
GROUP BY A.C_FUNDCODE, A.D_REGISTERDATE, A.C_FLAG, NVL(A.F_TOTALPROFIT,0), NVL(A.F_UNITPROFIT,0), NVL(A.F_TOTALPROFIT,0))
(需要在group by语句中将 A.C_FLAG这一项也添加进去)