[英]How to create new bucketed column from an existing continuous column in SQL?
[英]How to create a new column from existing column?
在表AZ中,我有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
如果我理解正確,請添加更多條件。 我認為最簡單的方法是使用子查詢:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.