简体   繁体   English

如何将此SAS数据步骤转换为条件计数器列的oracle sql?

[英]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. 下面的代码是我编写的可在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. 但是,由于数据步骤的运行方式与oracle sql的运行方式有关,我无法弄清楚如何在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 . 似乎您正在基于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. 请注意,在SQL中(与SAS不同),表表示无序集。 This means that you need a column that specifies the ordering. 这意味着您需要指定顺序的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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