我想知道为什么,对于我在这里的这个查询,为什么我必须对MAX()
语句使用case
聚合函数,而不是直接跳转到case语句:< / p>
select
bank_id,
tran_branch_code,
acct_sol_id,
acct_sol_name,
transaction_date,
gl_date,
transaction_id,
account_number,
max(case
when cast(substr(GLSH_Code,0,1) as int) >= 1
and cast(substr(GLSH_Code,0,1) as int) <= 5
and trans_type = 'D'
then (trans_amount)
--else 0
end ) Ind_Part_Tran_Dr_RBU,
max(case
when cast(substr(GLSH_Code,0,1) as int) >= 1
and cast(substr(GLSH_Code,0,1) as int) <= 5
and trans_type = 'C'
then (trans_amount)
--else 0
end) Ind_Part_Tran_Cr_RBU,
max(case
when cast(substr(GLSH_Code,0,1) as int) = 0
or (cast(substr(GLSH_Code,0,1) as int) >= 6
and cast(substr(GLSH_Code,0,1) as int) <= 9)
and trans_type = 'D'
then (trans_amount)
--else 0
end)Ind_Part_Tran_Dr_FCDU,
max(case
when cast(substr(GLSH_Code,0,1) as int) = 0
or (cast(substr(GLSH_Code,0,1) as int) >= 6
and cast(substr(GLSH_Code,0,1) as int) <= 9)
and trans_type = 'C'
then (trans_amount)
--else 0
end) Ind_Part_Tran_Cr_FCDU,
ccy_alias,
ccy_name,
acct_currency,
tran_currency
from
(
SELECT
DTD.BANK_ID,
DTD.SOL_ID Acct_Sol_ID, --Account Sol ID
dtd.br_code Tran_branch_code, -- branch code of the transacting branch
sol.sol_desc Acct_sol_name, -- name/description of SOL
DTD.TRAN_DATE Transaction_Date, --TransactionDate
DTD.GL_DATE GL_Date, --GL Date
TRIM(DTD.TRAN_ID) Transaction_ID, --Transaction ID
DTD.GL_SUB_HEAD_CODE GLSH_Code, --GLSH Code
dtd.tran_amt trans_amount,
GAM.ACCT_CRNCY_CODE Acct_Currency, --Account Currency
DTD.TRAN_CRNCY_CODE Tran_Currency, --Transaction Currency
cnc.crncy_alias_num ccy_alias,
cnc.crncy_name ccy_name,
GAM.FORACID Account_Number, --Account Number
DTD.TRAN_PARTICULAR Transaction_Particulars, --Transaction Particulars
DTD.CRNCY_CODE DTD_CCY,
--GSH.CRNCY_CODE GSH_CCY,
DTD.PART_TRAN_TYPE Transaction_Code,
--'Closing_Balance',
DTD.PSTD_USER_ID PostedBy,
CASE WHEN DTD.REVERSAL_DATE IS NOT NULL
THEN 'Y' ELSE 'N' END Reversal,
TRIM(DTD.TRAN_ID) REV_ORIG_TRAN_ID,
--OTT.REF_NUM OAP_REF_NUM,
'OAP_SETTLEMENT',
'RATE_CODE',
EAB.EOD_DATE
FROM TBAADM.DTD
LEFT OUTER JOIN TBAADM.GAM ON DTD.ACID = GAM.ACID AND DTD.BANK_ID = GAM.BANK_ID
LEFT OUTER JOIN TBAADM.EAB ON DTD.ACID = EAB.ACID AND DTD.BANK_ID = EAB.BANK_ID AND EAB.EOD_DATE = '24-MAR-2014'
left outer join tbaadm.sol on dtd.sol_id = sol.sol_id and dtd.bank_id = sol.bank_id
left outer join tbaadm.cnc on dtd.tran_crncy_code = cnc.crncy_code
WHERE DTD.BANK_ID = 'CBC01'
AND GAM.ACCT_OWNERSHIP = 'O'
AND GAM.DEL_FLG != 'Y'
--AND DTD.TRAN_DATE = '14-APR-2014'
AND DTD.TRAN_DATE between '01-APR-2014' and '21-APR-2014'
--and foracid in ('50010112441109','50010161635051')
--and DTD.SOL_ID = '5001'
and GAM.ACCT_CRNCY_CODE = 'USD'
)
group by
bank_id,
tran_branch_code,
acct_sol_id,
acct_sol_name,
transaction_date,
gl_date,
transaction_id,
account_number,
ccy_alias,
ccy_name,
Acct_Currency,
Tran_Currency
因为如果我删除MAX()
,我就会得到&#34;不是GROUP BY Expression&#34;,而且Toad指出我第一次出现GLSH_Code
。基于其他网站,解决此问题的方法是添加MAX()
功能。 我想了解为什么我应该使用该特定功能,它在查询中的确切功能,类似。
编辑:插入剩下的代码。
我确切知道MAX()
做了什么,它返回表达式中的最大值。但在这种情况下,我似乎无法确切地知道该函数试图返回的最大值是什么。
答案 0 :(得分:1)
GROUP BY
语句声明应汇总SELECT
中返回的所有列,但您希望将结果与GROUP BY
中列出的列分开。
这意味着我们必须在MIN, MAX, AVG, SUM, etc.
中列出 NOT 的任何列上使用GROUP BY
等汇总函数。
告诉SQL引擎当有多个选项时应该的结果是什么。
在一个简单的例子中,我们有一个包含三列的表:
PrimaryId SubId RowValue
1 1 1
2 1 2
3 2 4
4 2 8
如下所示的SQL(无效):
SELECT SubId, RowValue
FROM SampleTable
GROUP BY SubId
我们知道我们需要不同的SubId(因为GROUP BY
),但我们在汇总结果时不知道RowValue应该是什么。
SubId RowValue
1 ?
2 ?
我们必须在查询中明确,并指出RowValue应该是什么,因为结果可能会有所不同。
如果我们选择MIN(RowValue),我们会看到:
SubId RowValue
1 1
2 4
如果我们选择MAX(RowValue),我们会看到:
SubId RowValue
1 2
2 8
如果我们选择SUM(RowValue),我们会看到:
SubId RowValue
1 3
2 12
如果没有明确表示结果很可能是错误的,那么我们选择的SQL引擎通过强制执行聚合函数的需要来保护我们自己。
答案 1 :(得分:0)
除了Ind_Part_Tran_Dr_RBU,Ind_Part_Tran_Cr_RBU,Ind_Part_Tran_Dr_FCDU,Ind_Part_Tran_Cr_FCDU之外,您在所有列的末尾都有group by子句。在这种情况下,oracle希望您告诉如何处理这些列,即根据它找到的每个组聚合它们的函数。