楼主: jihezi927
13412 27

如何使用SAS中某一表的数据更新另一表中的数据 [推广有奖]

  • 1关注
  • 1粉丝

大专生

61%

还不是VIP/贵宾

-

威望
0
论坛币
256 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
599 点
帖子
47
精华
0
在线时间
63 小时
注册时间
2009-7-7
最后登录
2014-12-22

楼主
jihezi927 发表于 2013-5-16 13:40:19 |AI写论文
10论坛币
各位达人,我刚刚学习SAS,有个很棘手的问题。我有两个表,一个是所有产品的价格表,包括产品ID以及产品价格,另一个的每日销售的产品表, 也是包括产品ID以及价格,我每天会下载当日销售的产品表, 然后把价格升高3%,我希望我可以将升高的价格也更新到所有产品的价格表中。

我试过SQL的left join, merge, 好像都没办法。请问这种情况应该怎么办呢?

第一次发帖希望可以帮我解答一下。。。特悬赏10个论坛币。。多谢啦。

最佳答案

邓贵大 查看完整内容

Same idea @YobachiLiu, use subquery. And my previous comment was flawed, 'LEFT JOIN' works fine if used in the SELECT statement.
关键词:如何使用 数据更新 left join 10个论坛币 第一次发帖 产品 价格表 如何
千江有水千江月

沙发
邓贵大 发表于 2013-5-16 13:40:20
Same idea @YobachiLiu, use subquery.
  1. data table1;
  2.         input client $6. paidclaims;
  3. cards;
  4. 123456 1000
  5. 234567 2000
  6. aaaaaa 3333
  7. ;
  8. data table2;
  9.         input client $6. totpaidclaims;
  10. cards;
  11. 123456 5000
  12. 234567 9000
  13. bbbbbb 2222
  14. ;

  15. proc sql;
  16.         update table1
  17.                 set paidclaims = (
  18.                         select totpaidclaims from table2
  19.                                 where table1.client=table2.client
  20.                 )
  21.                 where client in (
  22.                         select client from table2
  23.                 );
  24. quit;
复制代码
And my previous comment was flawed, 'LEFT JOIN' works fine if used in the SELECT statement.
  1. proc sql;
  2.         create table table3 as
  3.                 select a.client, coalesce(b.totpaidclaims, a.paidclaims) as paidclaims
  4.                 from table1 a left join table2 b
  5.                         on a.client=b.client;
  6. quit;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
boe + 1 + 1 + 1 sql用的这么好,佩服!

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

藤椅
milos_luna 发表于 2013-5-16 14:39:50
我没太明白什么意思其实,所以胡乱回答个,呵呵

data one ;
                input id price ;
                cards;
                11 20
                12 30
                13 40
                ;
                run;



data two;
                id = 12 ;
                price = 33;
                run;


Proc SQL;
                create table three as
                select a.*, b.price as new_price from one as a left join two as b
                on a.id = b.id;
                quit;

板凳
milos_luna 发表于 2013-5-16 14:40:25
乱写的,我也不会的

报纸
zkymath 在职认证  发表于 2013-5-16 14:58:14
update

地板
alicejohn 发表于 2013-5-16 16:10:49
数据库的工作,级联更新,需要触发器,很麻烦,我好奇为什么不把产品价格单独弄出来一个表呢,用的时候只要关联下就可以了

7
YobachiLiu 发表于 2013-5-16 20:47:24
update 产品表 set 价格=价格*1.03
where id in(select id from 销售的产品表);
如果这是个重复性的或者有规律的计算,你可以创建过程或者数据库job或者时间触发器的形式,让服务器系统自动更新,就不用你每天做了

8
yuerqieqie 发表于 2013-5-16 21:32:06
不明白为什么SQL的left join, merge不可以实现。

9
邓贵大 发表于 2013-5-16 22:06:48
"然后把价格升高3%", 奸商!
'left join' no good because PROC SQL doesn't support the syntax in the UPDATE statement.
merge should be OK
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

10
jihezi927 发表于 2013-5-17 01:29:01
milos_luna 发表于 2013-5-16 14:39
我没太明白什么意思其实,所以胡乱回答个,呵呵

data one ;
呵呵。理解力很厉害。这个方法是可以的,不过就是会多加一列NEW-Price出来,有什么办法可以只更新那个所有产品的价格表呢?
千江有水千江月

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 06:32