楼主: Shirubei
1640 8

SAS SQL问题,进来挑战一下吧 [推广有奖]

  • 0关注
  • 0粉丝

高中生

32%

还不是VIP/贵宾

-

威望
0 级
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
1 点
信用等级
0 点
经验
151 点
帖子
20
精华
0
在线时间
29 小时
注册时间
2012-9-6
最后登录
2020-11-29

楼主
Shirubei 发表于 2013-2-21 09:42:35 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
昨天碰到一个需要需要在SAS DIS环境中实现以下的SQL,想了半天没解决,还请各位高手指点指点。问题描述如下,受注表Order中,主键为受注番号OrdID和受注明细号DetailID,同时还有2个字段,一个为用户ID(CID),一个为受注日(OrderDate)。需要得到每条数据的前回受注日LastOrdDate。也就是说,用本条数据的用户ID,再去查找一次受注表,找到所有受注日比当前记录小的数据中受注日最大的那一个日期。
实例数据如下
OrdID     DetailID    CID      OrderDate
1001      1              C001    20120605
1001      2              C001    20120713
1002      1              C001    20120827
1002      2              C001    20120827
1003      1              C002    20120614
1003      2              C002    20120709  

期望结果
OrdID       DetailID    CID        OrderDate    LastOrdDate
1001        1            C001      20120605   
1001        2            C001      20120713    20120605
1002        1            C001      20120827    20120713
1002        2            C001      20120827    20120713
1003       1             C002      20120614   
1003       2              C002    20120709    20120614

多谢您的关注与参与!



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:sql detail Order Etail taili 最大的 记录 用户

沙发
playmore 发表于 2013-2-21 12:50:50
你的问题挺简单的,在一个data步里用lag函数,再把不符合条件(不同的CID)的删去就好
data want;
set have;
by CID OrdDate;
LastOrdDate=lag(OrdDate)
if first.OrdDate then LastOrdDate=.;
run;

也可以用proc expand来做,那个要规范些,并且不但可以lag,还可以lead
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

藤椅
Shirubei 发表于 2013-2-22 12:13:40
thank you for replying. We could only use SQL under the DIS environment.

板凳
pobel 在职认证  发表于 2013-2-22 13:54:41
proc sql;
   create table wanted as
    select distinct l.*,max(r.orderdate) as lastorddate format=yymmdd8.
           from test as l left join test as r
            on l.cid=r.cid and l.orderdate>r.orderdate
         group by l.cid,l.orderdate;
quit;
和谐拯救危机

报纸
luling2010 发表于 2013-2-22 14:35:36
楼上正解,崇拜!

地板
luling2010 发表于 2013-2-22 14:46:47
求教 pobel  
proc sql;
    create table wanted as
     select distinct l.*,max(r.orderdate) as lastorddate format=yymmdd8.
            from test as l , test as r
             where l.cid=r.cid and l.orderdate>r.orderdate
          group by l.cid,l.orderdate;
quit;

和您的程序比起来,好像lastorder缺失值的没有了,请问二者的区别在哪里,谢谢!

7楼
tangliang0905 发表于 2013-2-23 01:00:59
since "where" is used, so only records met the "where" criteria will be listed, that is to say, only the records having newly order date will be kept.

8楼
Shirubei 发表于 2013-2-25 14:07:04

Answer

with help from a friend, I finished it as below and it proved correct.

   create table wanted as
    select A.*,
           ( select max(r.orderdate)
             from Order as B
             where A.CID = B.CID
             and   A.orderdate > B.orderdate
           ) as lastorddate format=yymmdd8.
           from Order as A ;
quit;

Thank you all !

9楼
luling2010 发表于 2013-2-25 20:43:45
请教各位大牛,这个lastorderdate空值怎么出来的啊?

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-11-20 15:09