简体   繁体   中英

How can I convert this SAS datastep to oracle sql for a conditional counter column?

I apologize for the generalness of the question. I'm trying to create a column that groups rows together based on the time between the current and previous observation. The code below is code that I' wrote that works correctly in SAS. However because of the way that a data step runs vs how oracle sql runs I can't figure out how to do this in oracle sql. Any help would be greatly appreciated!

DATA GROUP;
SET LAG1;
BY CUSTOMER_KEY;

IF (TIME_BTW>5 OR TIME_BTW=.) THEN JOURNEY=0;
JOURNEY+1;
IF FIRST.CUSTOMER_KEY THEN GROUP=0;
IF JOURNEY=1 THEN GROUP+1;
RUN;

It looks like you are defining groups based on time_btw . You seem to want an analytic function. I think the code is like this:

select t.*,
       sum(case when time_btw > 5 then 1 else 0 end) over (partition by customer_key order by ??) as grp
from t;

Note that in SQL (unlike SAS), tables represent unordered sets. This means that you need a column that specifies the ordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM