sql - SAS SQL 分层查询

标签 sql sas hierarchical-data

我正在尝试编写一个 sql 查询 (proc sql) 以从示例表中获取数据:

order_id            base_order_id           customer_id
==========================================================
1                       null                            1  //only one transaction
-------------------------------------------------------------------------------
2                       null                            1             //order_start
3                       2                               1
4                       3                               1             
5                       4                               1
6                       5                               1
7                       6                               1             //order_end
-------------------------------------------------------------------------------

as follows:

order_id            last_order_id           customer_id 
1                       null                            1                   
2                       7                               1                       

Let me put it this way. Order_id 2 has 6 subprocesses. We can assume that complete order for that one Client consists of order_id from 2 to 7. Order start = order_id 2 as whole order ends at order_id 7.

I'm begginer in sas sql. I've tried joining the same table via left join, "having" clause but nothing worked well. Is there any way to get a query result as in table 2?

Thank you in advance.

EDIT2. SQL I wrote, that brings closest result.

SELECT t1.order_id, t1.base_order_id as last_order_id, t1.customer_id

FROM table1 t1

GROUP BY t1.order_id
     HAVING (t1.order_id = max(t1.base_order_id) 
     or t1.base_order_id IS NULL)

最佳答案

正如 Gordon 在评论中所说,我想不出在 PROC SQL 中执行此操作的方法。

然而,执行此操作的“SAS”方法是连通分量分析。 SAS/OR 中的 PROC OPTNET 就是这样做的。

data have;
input order_id base_order_id customer_id;
datalines;
1 . 1
2 . 1
3 2 1
4 3 1
5 4 1
6 5 1
7 6 1
;
run;

/*Connect the first order to itself*/
data have;
set have;
if base_order_id = . then base_order_id = order_id;
run;

/*Use SAS/OR and connected components*/
proc optnet
    data_links = have(rename=(order_id = to base_order_id = from))
    out_nodes = out;
    concomp;
run;

/*Summarize and add customer id*/
proc sql noprint;
create table want as 
select a.order_id,
       a.last_order_id,
       b.customer_id
    from (
        select min(node) as order_id,
               max(node) as last_order_id
            from out
            group by concomp
    ) as a
      left join
    test as b
      on a.order_id = b.order_id;
quit;

这将返回您在 WANT 数据集中查找的内容。

关于sql - SAS SQL 分层查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27381061/

相关文章:

MySQL 批量 INSERT .. ON DUPLICATE KEY UPDATE,同时指定各个列

sas - 刷新 PDV 变量

c++ - 什么会导致像这样的 'doubled'堆栈?

mongodb - MongoDB 中 "Parent Links"树上的 MapReduce

sql - 对嵌套集中的 sibling 进行排序

sql - IF ELSE 为join 的条件?

SQL合并具有相同ID但列值不同的两行(Oracle)

count - SAS Enterprise Guide 计数 IF

sql - 使用sql检测循环

mysql - SQL:从带有变量的表中选择