坚持使用此SQL摘要功能

时间:2017-06-16 19:10:59

标签: sql sas

在SAS SQL中生成汇总值的练习让我整天都在弯道。它的SAS部分基本上只是我嘲笑一些数据,如果你想要回答一般的SQL原则,那么适用一般的SQL原则。

考虑以下内容:

输入数据......

data test;
account_rk = 111111;
customer_rk = 111111;
ca_flag = 0;
score1 = 100;
score_with_ca = 0;
score_without_ca = 100;
output;
account_rk = 111111;
customer_rk = 222222;
ca_flag = 1;
score1 = 150;
score_with_ca = 150;
score_without_ca = 0;
output;
run;

尝试汇总功能......

proc sql;
create table test2 as 
select
account_rk,
sum(ca_flag) as sum_ca_flag,
    min(case
    when score_with_ca ne 0 then score_with_ca
    else score1 end) as min_score_with_ca,
    min(case 
    when score_without_ca eq 0 then score_without_ca
    else score1 end) as min_score_without_ca
from test
group by account_rk
having sum_ca_flag > 0;
quit;

想要的东西应该是什么样的......

data test3;
account_rk = 111111;
sum_ca_flag = 1;
min_score_with_ca = 150;
min_score_without_ca = 100;
output;
run;

我需要在SQL中更改以获得看起来像第三步生成的输出的输出?逻辑步骤中的想法是根据score1的值生成ca_flag的最小值,并按account_rk对其进行分组。

谁能告诉我我做错了什么?

由于

1 个答案:

答案 0 :(得分:1)

我认为你只需要NULL或没有else条款,然后是正确的逻辑:

proc sql;
create table test2 as 
    select account_rk, sum(ca_flag) as sum_ca_flag,
           min(case when score_with_ca > 0 then score_with_ca
               end) as min_score_with_ca,
           min(case when score_without_ca > 0 then score_without_ca
               end) as min_score_without_ca
    from test
    group by account_rk
    having sum_ca_flag > 0;
quit;