2014-09-09 5 views
1

테이블에서 나는 3 개의 열에 a, b, c라고 말합니다. || 여기서 c는 d의 별칭입니다. 어떻게 열 C에서 "E"를 말할 같은 테이블에 새 열을 생성하는기존 열에서 새 열을 만드는 방법은 무엇입니까?

select 
    a.lyl_id_no, 
    sum(a.trn_tot_prc) as PURCH, 
    sum(case when a.trn_dt > current_date - 365 then 1 else 0 end) cnt_trips_1yr , 
from abc a 
group by 1 

결과 : 내가 말을해야 같은 테이블에 새 열을 필요

a.lylid purch cnt_trips_lyr 
123  12   4 
242  10   1 

그러나 cnt_trips_1yr> 3, cnt_trips_1yr > 2

+0

질문이 명확하지 않습니다. –

답변

0

제가 올바르게 이해하면 더 많은 조건을 추가하십시오. 가장 쉬운 방법은 하위 쿼리를 사용하는 것입니다.

select a.*, 
     (case when cnt_trips_1yr > 3 then 1 else 0 end) as IsCntGt3, 
     (case when cnt_trips_1yr > 2 then 1 else 0 end) as IsCntGt2 
from (select a.lyl_id_no, 
      sum(a.trn_tot_prc) as PURCH, 
      sum(case when a.trn_dt > current_date - 365 then 1 else 0 end) as cnt_trips_1yr 
     from abc a 
     group by a.lyl_id_no 
    ) a; 
관련 문제