检索SQL或SAS中的一组数据的子集

时间:2018-01-04 05:30:52

标签: sql sql-server sas

我的数据集如下表所示。

ARR INST DUE_DATE
1   1   1-Dec
1   2   8-Dec
1   3   15-Dec
1   4   22-Dec
2   1   1-Dec
2   2   8-Dec
3   1   5-Dec
3   2   12-Dec
3   3   19-Dec
4   1   6-Nov
4   2   13-Nov
4   3   20-Nov
4   4   27-Nov
4   5   4-Dec
4   6   11-Dec
5   1   1-Jan
5   2   7-Jan
5   3   13-Jan
5   4   20-Jan
5   5   27-Jan
5   6   3-Feb
5   7   10-Feb
5   8   17-Feb
5   9   23-Feb
5   10  24-Feb

我需要根据支付的分期付款数量检索每项安排的数据。

EG。如果特定安排的总分期数不小于或等于4,则输出应具有所有分期付款的值,直到第4期。

如果它大于四,或四的倍数,则值应为下一组四个值。

输出应该是

ARR INST DUE_DATE
1   1   1-Dec
1   2   8-Dec
1   3   15-Dec
1   4   22-Dec
2   1   1-Dec
2   2   8-Dec
3   1   5-Dec
3   2   12-Dec
3   3   19-Dec
4   5   4-Dec
4   6   11-Dec
5   9   23-Feb
5   10  24-Feb

如何在SQL Server或SAS Enterprise Guide中获取此输出?

感谢。

3 个答案:

答案 0 :(得分:2)

你可以使用它。

DECLARE @MyTable TABLE (ARR INT, INST INT, DUE_DATE VARCHAR(10))
INSERT INTO @MyTable VALUES
(1 , 1 , '1-Dec '),
(1 , 2 , '8-Dec '),
(1 , 3 , '15-Dec'),
(1 , 4 , '22-Dec'),
(2 , 1 , '1-Dec '),
(2 , 2 , '8-Dec '),
(3 , 1 , '5-Dec '),
(3 , 2 , '12-Dec'),
(3 , 3 , '19-Dec'),
(4 , 1 , '6-Nov '),
(4 , 2 , '13-Nov'),
(4 , 3 , '20-Nov'),
(4 , 4 , '27-Nov'),
(4 , 5 , '4-Dec '),
(4 , 6 , '11-Dec'),
(5 , 1 , '1-Jan '),
(5 , 2 , '7-Jan '),
(5 , 3 , '13-Jan'),
(5 , 4 , '20-Jan'),
(5 , 5 , '27-Jan'),
(5 , 6 , '3-Feb '),
(5 , 7 , '10-Feb'),
(5 , 8 , '17-Feb'),
(5 , 9 , '23-Feb'),
(5 , 10, '24-Feb'),
(5 , 11, '25-Feb'),
(5 , 12, '26-Feb'),
(6 , 1, '27-Feb')


DECLARE @numofinst INT = 4

SELECT ARR, INST, DUE_DATE FROM (
    SELECT *, 
        RN = ROW_NUMBER() OVER(PARTITION BY ARR ORDER BY INST ),
        CNT = COUNT(*) OVER(PARTITION BY ARR ) 
    FROM @MyTable
) AS T
WHERE 
    RN  > (( CEILING( CAST( CNT AS decimal(18,2) ) / CAST( @numofinst  AS decimal(18,2) )) - 1 ) * @numofinst)

结果:

ARR         INST        DUE_DATE
----------- ----------- ----------
1           1           1-Dec 
1           2           8-Dec 
1           3           15-Dec
1           4           22-Dec
2           1           1-Dec 
2           2           8-Dec 
3           1           5-Dec 
3           2           12-Dec
3           3           19-Dec
4           5           4-Dec 
4           6           11-Dec
5           9           23-Feb
5           10          24-Feb
5           11          25-Feb
5           12          26-Feb
6           1           27-Feb

答案 1 :(得分:0)

对于已排序的SAS数据集或提供有序数据的远程数据源,以下DATA Step示例显示了双DOW循环如何识别和输出属于每个id的最后4行块的行:

data want(label="Rows from each ids last 4-row chunk");
  do _n_ = 0 by 1 until (last.id);
    set have;
    by id sequence; %* by sequence not strictly necessary, but enforces the expectation of increasing sequence within id;
  end;

  _out_from_n = floor ( _n_ / 4 ) * 4;

  do _n_ = 0 to _n_;
    set have;
    if _n_ >= _out_from_n then OUTPUT;
  end;

  drop _:;
run;

答案 2 :(得分:0)

或者,您可以按Richard修改代码,以便对SET Statement POINT= option使用随机读取权限,如下所示:

data want;
    retain point 1;
    drop point;
    do _n_ = 0 by 1 until (last.arr);
        set have;
        by arr inst;
    end;
    do point = point+(floor(_n_/4)*4) to point+_n_;
        set have point=point;
        output;
    end;        
run;