2017-10-30 1 views
0

하위 문자열로 그룹화하려고하는데 별칭을 사용할 수 없다는 것을 알고 있지만이 경우에도 결과가 나오지 않습니다.Substr을 사용하여 SQL 그룹 By

select 
substr(cd_orig_bic,5,2) cd, 
case 
when substr(CD_TXN_TYPE,1,1) = '1' then 'a' 
    when substr(CD_TXN_TYPE,1,1) = '2' then 'b' 
    else 'OTHER' 
    end txn_type, 
d_booking, 
d_value, 
d_execution, 
from c.c_t_transaction_queue a join c.c_d_currency b on a.id_currency=b.id_currency 
where 
d_effective>=to_date('01.01.2017','DD.MM.YYYY') 
and 
d_effective<=to_date('30.09.2017','DD.MM.YYYY') 
and substr(cd_orig_bic,5,2)!='SK' 
group by substr(cd_orig_bic,5,2); 
+1

어느 [DBMS] (https://en.wikipedia.org/wiki/DBMS) 제품이 사용중인 ? 포스트그레스? 신탁? "_SQL_"은 쿼리 언어 일뿐 특정 데이터베이스 제품의 이름은 아닙니다. –

+0

제품 고유의 기능이 많이 있습니다 ... 그리고 GROUP BY는 유효한 ANSI SQL이 아닙니다. – jarlh

+0

안녕하세요 .. 오라클 SQL 개발자 – LuciaK

답변

0

쿼리에 집계 함수가 없습니다. 여기

는 작품 "에 의해 그룹"방법입니다

SELECT sum(column1) -- here is a aggregation function 
     ,column2 
FROM table 
GROUP BY column2 -- here is the column you want to aggregate on 

그 결과 당신은 자세한 내용은 this article를 읽을 수 있습니다 열이

의 각 값에 대해, 컬럼의 합계입니다.

0

왜 쿼리에서 집계 함수가 사용되지 않으므로 데이터별로 그룹화하려고합니까?

고유 값을 원하면 고유 키워드를 사용하여 고유 값을 가질 수 있습니다.

select distinct 
substr(cd_orig_bic,5,2) cd, 
case 
when substr(CD_TXN_TYPE,1,1) = '1' then 'a' 
    when substr(CD_TXN_TYPE,1,1) = '2' then 'b' 
    else 'OTHER' 
    end txn_type, 
d_booking, 
d_value, 
d_execution, 
from c.c_t_transaction_queue a join c.c_d_currency b on a.id_currency=b.id_currency 
where 
d_effective>=to_date('01.01.2017','DD.MM.YYYY') 
and 
d_effective<=to_date('30.09.2017','DD.MM.YYYY') 
and substr(cd_orig_bic,5,2)!='SK'; 

또한 집계 함수를 원한다면 위 쿼리를 내부 쿼리로 사용하고 외부 쿼리에서 group by를 사용할 수 있습니다.

select cd,txn_type,d_booking,sum(d_value) as value, 
d_execution from (select 
    substr(cd_orig_bic,5,2) cd, 
    case 
    when substr(CD_TXN_TYPE,1,1) = '1' then 'a' 
     when substr(CD_TXN_TYPE,1,1) = '2' then 'b' 
     else 'OTHER' 
     end txn_type, 
    d_booking, 
    d_value, 
    d_execution, 
    from c.c_t_transaction_queue a join c.c_d_currency b on a.id_currency=b.id_currency 
    where 
    d_effective>=to_date('01.01.2017','DD.MM.YYYY') 
    and 
    d_effective<=to_date('30.09.2017','DD.MM.YYYY') 
    and substr(cd_orig_bic,5,2)!='SK' 
) group by cd,txn_type,d_booking,d_execution; 
+0

안녕하세요, 몇 천 라인 반복적으로 사용되는 국가 코드이므로 "cd"로 그룹화해야합니다. – LuciaK

+0

집계 함수가 없으면 group by를 사용할 필요가 없습니다. "그룹화 기준"의 목적은 집계 함수를 사용할 때 레코드를 그룹화하는 것입니다. 귀하의 요구 사항을 더 자세히 설명해주십시오. –

+0

안녕하세요, 국가 코드보다 너무 많은 결과가 있기 때문에 cd (국가 코드)로 그룹이 필요합니다. – LuciaK