[英]How to replace/overwrite values
If I have a table like this called myTable:如果我有一个这样的表叫做 myTable:
| Var1 | Var2 | Var3 | Var4 | Var5 |
+-------+------+------+------+------+--------+
| Name1 | 1.1 | 1.2 | 0 | 03 | 201805 |
| Name1 | 1.1 | 1.2 | 0 | 03 | 201803 |
| Name2 | 2.1 | 2.2 | 0 | 03 | 202001 |
| Name3 | 3.1 | 3.2 | 3 | 01 | 202002 |
How would I write a query that overwrite/updates all Name1
rows with the Var1
and Var2
values from Name2
given that the rest of the variables matches and only do it on the condition that Var5
is in a desired time interval, like 201805
-> 202001
?我将如何编写一个查询重写/更新所有
Name1
行与Var1
和Var2
从数值Name2
给出了变量匹配的休息,只能做它该条件Var5
是在期望的时间间隔,如201805
- > 202001
?
In SAS Proc SQL
the query is a bit arduous.在 SAS
Proc SQL
,查询有点困难。
data have;
input
name $ Var1 Var2 Var3 Var4 Var5: yymmn6.;
format var5 yymmn6.;
datalines;
Name1 1.1 1.2 0 03 201805
Name1 1.1 1.2 0 03 201803
Name2 2.1 2.2 0 03 202001
Name3 3.1 3.2 3 01 202002
;
proc sql;
create table rewrites as
select two.var1, two.var2
from have as one join have as two
on one.var3 = two.var3
& one.var4 = two.var4
where one.name = 'Name1' and two.name = 'Name2'
& one.var5 between '01may2018'd and '31jan2020'd
& two.var5 between '01may2018'd and '31jan2020'd
;
update have
set
var1=(select var1 from rewrites)
, var2=(select var2 from rewrites)
where name = 'Name1'
& var5 between '01may2018'd and '31jan2020'd
;
quit;
%let syslast = have;
Have a look at the SQL 'LEAD()' function.看看 SQL 'LEAD()' 函数。
There are some nice examples here Example from this URL:有一些很好的例子在这里实例从这个网址:
SELECT
month,
brand_name,
net_sales,
LEAD(net_sales,1) OVER (
PARTITION BY brand_name
ORDER BY month
) next_month_sales
FROM
sales.vw_netsales_brands
WHERE
year = 2018;
Basically you can select the values from the next row.基本上您可以从下一行中选择值。
For the condition on var5, simply put it in the WHERE clause.对于 var5 上的条件,只需将其放在 WHERE 子句中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.