按组分组的单个记录的SQL查询操作

时间:2014-07-16 17:17:36

标签: sql sas

我是sql的新手,请帮忙。 我有一张桌子 像:

ID  account bad amount 
1   100      1   1000
1   10001    0    0
2   211      0    0
2   21101    1   700
2   21102    0    0
5   123      0    0
5   12301    0    0
5   12302    1    100
3   111      0    0
3   11102    0    0
4   1213     1   600
4   121302   1   700

我想得到的输出是:

ID account bad amount
1    100    1  1000
2    21101  1  700
4    1213   1  600

这里的规则是:对于ID,它可以有多个帐户(父帐户和子帐户,即父帐户ID加'01'或'02'或'03'....)。父帐户不是以'01'或'02或'03'结尾。所以对于ID,它被定义为坏(坏= 1)

1) if its parent account is bad
2) else if sub account is bad with amount > 500
3) for parent account and sub account both are bad, take parent account.

谢谢!

1 个答案:

答案 0 :(得分:0)

我知道你使用PROC SQL程序要求这个,但我不知道如何做到这一点。但是,我已经使用可能有用的数据步骤编写了一些代码。

希望这会有所帮助;

proc sort data=input_dataset;
    by ID;
run;

data step1;

    set input_dataset;

    account2 = compress(put(account,8.));
    length_acc = length(compress(put(account,8.)));
    if length_acc >= 2 then 
        last_two = substr(account2, length_acc-1, 2);

    if last_two IN ("01", "02", "03") then
        parent_acc = "N";
    else parent_acc = "Y";

    if parent_acc = "Y" and bad = 1 then
        score = 0;

    else if parent_acc = "N" and bad = 1 and amount >= 500 then
        score = 0;

    else score = 1000;

run;


proc sort data=step1;
    by ID score descending parent_acc;
run;


data step2;
    set step1;
    by ID;
    if first.ID and score = 0 then
        output;
    keep ID account bad amount;
run;