テーブルAZには、a、b、cと言う3つの列があります|| ここで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と表示される新しい列が必要です。
正しく理解できたら、条件を追加してください。最も簡単な方法は、サブクエリを使用することだと思います。
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;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加