2014-04-23 2 views
-1

여기 왜이 쿼리에서 집계 함수를 사용하여 case 문을 사용해야하며 case 문으로 바로 이동하지 않는지 궁금합니다.GROUP BY가 아닌 표현식 및 집계 함수

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()을 제거한다면, 나는 "표현 BY 아니 GROUP"을 얻을 것, 그리고 두꺼비는 GLSH_Code의 첫 번째 항목에 저를 지적 때문입니다. 다른 웹 사이트를 기반으로, 이것에 대한 치료법은 실제로 MAX() 기능을 추가하는 것입니다. 그 특정 기능을 사용해야하는 이유를 정확히 이해하고 싶습니다. 쿼리에서 정확히 무엇을합니까?과 같은 것들입니다.

편집 : 나머지 코드를 삽입하십시오.

MAX()이 무엇인지 알고 있습니다. 식에서 가장 큰 값을 반환합니다. 그러나이 경우 함수가 반환하려고하는 최대 값이 무엇인지 정확하게 파악할 수 없습니다.

+0

검색어가 절반 인 것처럼 보입니까? FROM 절은 어디에 있습니까? –

+0

@IleshPatel이 원래 게시물을 편집했지만 이제는 나머지 코드가 포함되어 있습니다 – JamesP

+0

[ora-00979 GROUP BY 표현이 아님] (http://stackoverflow.com/questions/1520608/ora-00979-not-a) -group-by-expression) – Ben

답변

0

Ind_Part_Tran_Dr_RBU, Ind_Part_Tran_Cr_RBU, Ind_Part_Tran_Dr_FCDU, Ind_Part_Tran_Cr_FCDU를 제외한 모든 열의 끝에 group by 절이 있습니다. 이 경우 오라클은이 열을 사용하여 수행 할 작업, 즉 발견 한 모든 그룹에 대해 어떤 기능을 집계해야 하는지를 알려줍니다.

1

에 반환 된 모든 열을 집계해야하지만 결과는 GROUP BY에 나열된 결과로 구분해야한다고 선언합니다.

즉 과 같은 집계 함수를 이 아닌GROUP BY에 나열해야합니다.

두 개 이상의 옵션이있는 경우 예상 결과 으로 설정해야한다는 것을 SQL 엔진에 알리는 것입니다.

PrimaryId SubId RowValue 
1   1  1 
2   1  2 
3   2  4 
4   2  8 

그리고 (무효 인) 다음과 같은 SQL : 우리는 우리가 하위 ID 년대 별개을 원하는지

SELECT SubId, RowValue 
    FROM SampleTable 
    GROUP BY SubId 

간단한 예에서

, 우리는 세 개의 열이있는 테이블이 ( GROUP BY 때문에) 우리는 결과를 집계 할 때 RowValue가 무엇인지 알지 못합니다.

SubId RowValue 
1  ? 
2  ? 

우리는 쿼리에서 명시 적이어야하며 결과가 다를 수 있으므로 RowValue가 무엇인지 표시해야합니다.우리는 MIN (RowValue)를 선택하면

우리는 다음을 참조하십시오 우리가 MAX (RowValue)를 선택하면

SubId RowValue 
1  1 
2  4 

우리가 참조 : 우리는 (RowValue를) SUM을 선택하면

SubId RowValue 
1  2 
2  8 

우리가 참조 :

SubId RowValue 
1  3 
2  12 

명백히 밝혀지지는 않았지만 결과가 잘못 될 가능성이 높습니다. 따라서 우리의 선택적인 SQL 엔진은 oursel으로부터 우리를 보호합니다. ves는 집계 함수의 필요성을 강요합니다.

관련 문제