我希望每个分行只选择3个OBS,在这里我需要回答以下规则:
如果分支机构只有2个账户--如果分支机构中只有2个最高收入,则为第一个分支机构的2个最高收入帐户,如果该分支机构中有3个帐户,则为第二个帐户twice

发布于 2020-11-22 12:39:25
嗯嗯。。。row_id列似乎是在命令帐户内的收入。因此,您应该能够使用proc sql,尽管它有点混乱:
select t.*
from t join
(select crm_branch_id, count(distinct account_id) as cnt
from t
group by crm_branch_id
) b
on b.crm_branch_id = t.crm_branch_id
where (cnt = 1 and t.row_id <= 3) or
(cnt = 2 and t.row_id = 1 or
cnt = 2 and t.row_id = 2 and
t.income = (select max(t2.income)
from t t2
where t2.crm_branch_id = t.crm_branch_id and
t2.row_id = 2
)
) or
(cnt = 3 and row_id = 1) or
(cnt > 3 and row_id = 1 and
(select count(*)
from t t2
where t2.crm_branch_id = t.crm_branch_id and
t2.row_id = 1 and
t2.income >= t.income
) <= 3
);where子句中的神秘逻辑是处理不同数量的帐户:
如果有一个帐户,则取前三行。如果有两个帐户,则使用row_id = 2.
row_id = 2.
row_id = 1.
row_id = 1的行。然后以收入为基础,选出前三名。--发布于 2020-11-22 16:44:18
陶氏处理可以执行肤色选择。
branch
account的计数数,根据规则输出的顶级income,branch和示例:
data have;
input income account_id branch_id seq_act;
datalines;
1224932 123 358 1
700400 123 358 2
646730 123 358 3
644677 123 358 4
2017 123 358 5
11338320 567 358 1
3806060 567 358 2
3642089 567 358 3
1403174 567 358 4
400530 567 358 5
;
/* presume data is
* - contiguous by branch and account
* - descending income
* - ascending seq_act
*/
data want(drop=i n);
* count number of accounts in branch;
do until (last.branch_id);
set have;
by branch_id account_id notsorted descending income /*ascending*/ seq_act;
n + first.account_id;
end;
do until (last.branch_id);
set have;
by branch_id account_id notsorted;
i + first.account_id;
select (n);
when (1) if seq_act <= 3 then output; /* first 3 when 1 account */
when (2) if seq_act <= 3-i then output; /* first 2 then first 1 when 2 accounts */
otherwise if seq_act = 1 then output; /* first 1 from each account */
end;
end;
i = 0;
n = 0;
run;输出

https://stackoverflow.com/questions/64953564
复制相似问题