Sql 不是按表达式分组&;聚合函数
我想知道,对于我这里的查询,为什么我必须对Sql 不是按表达式分组&;聚合函数,sql,oracle,toad,Sql,Oracle,Toad,我想知道,对于我这里的查询,为什么我必须对case语句使用MAX()聚合函数,而不是直接跳转到case语句中: 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
case
语句使用MAX()
聚合函数,而不是直接跳转到case语句中:
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
选择
银行id,
交易分公司代码,
账户sol\u id,
账户名称,
交易日,
gl_日期,
交易id,
账号,
马克斯(凯斯)
强制转换时(substr(GLSH_代码,0,1)为int)>=1
and cast(substr(GLSH_代码,0,1)as int)=1
和转换(substr(GLSH_代码,0,1)为int)=6
和转换(substr(GLSH_代码,0,1)为int)=6
和转换(substr(GLSH_代码,0,1)为int)除了Ind_Part_Tran_Dr_RBU、Ind_Part_Tran_Cr_RBU、Ind_Part_Tran_dru FCDU、Ind_Part_Tran_Cr_FCDU之外,所有列的末尾都有group by子句。在这种情况下,oracle希望您告诉您如何处理这些列,即根据它必须为找到的每个组聚合它们的函数。group by
语句t声明应聚合选择中返回的所有列,但您希望通过分组依据中列出的列来分隔结果
这意味着我们必须在组中列出的以外的任何列上使用聚合函数,如MIN、MAX、AVG、SUM等
它是关于告诉SQL引擎当有多个选项时,预期的结果应该是什么
在一个简单的示例中,我们有一个包含三列的表:
PrimaryId SubId RowValue
1 1 1
2 1 2
3 2 4
4 2 8
和以下类似的SQL(无效):
我们知道我们需要不同的SubId(因为groupby
),但我们不知道在聚合结果时RowValue应该是什么
SubId RowValue
1 ?
2 ?
我们必须在查询中明确,并指出RowValue应该是什么,因为结果可能会有所不同
如果我们选择MIN(RowValue),我们会看到:
如果我们选择MAX(RowValue),我们会看到:
如果我们选择SUM(RowValue),我们会看到:
如果不明确,结果很可能是错误的,因此我们选择的SQL引擎通过强制执行聚合函数来保护我们不受影响。这似乎是查询的一半?where is from子句?@IleshPatel编辑了我的原始帖子,现在它包含了其余可能重复的代码对于@Ben,我不这么认为。我还想问为什么我应该使用这样的函数,以及这些函数在添加到查询中时到底做了什么
SubId RowValue
1 ?
2 ?
SubId RowValue
1 1
2 4
SubId RowValue
1 2
2 8
SubId RowValue
1 3
2 12