gpt4 book ai didi

sql - SAS SQL 分层查询

转载 作者:行者123 更新时间:2023-12-02 01:42:58 27 4
gpt4 key购买 nike

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

order_id            base_order_id           customer_id==========================================================1                       null                            1  //only one transaction-------------------------------------------------------------------------------2                       null                            1             //order_start3                       2                               14                       3                               1             5                       4                               16                       5                               17                       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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com