我搜索了网站,有一些解决方案可以更新另一个df,但他们没有给我一个解决方案,可以使用多个变量作为标准。
我有一个数据库,其中包含信息,我想要做的是用支出摘要更新特定列表。
df1:
Invoice Supplier OA Period Spend
1 A A1 P1 100
2 A 0 P1 500
3 A A1 P1 300
4 A A1 P2 50
5 B 0 P3 25
6 B 0 P3 20
7 B 0 P3 5
8 C C5 P1 30
9 C C5 P1 35
10 C D10 P1 5
df2
Supplier OA
A 0
A A1
B 0
C C5
结果预期
df2
Supplier OA Spend-P1 Spend-P2 Spend-P3 Total
A 0 500 500
A A1 400 50 450
B 0 50 50
C C5 65 65
我尝试过使用更新sql查询
sqldf("UPDATE df1, df2 SET df2.[Spend-P1] = sum(Spend) WHERE dfa.[OA] = df2.[OA] and df1.[Supplier] = df2.[Supplier] and df1.[Period] = 'P1'")
但我收到错误消息
rsqlite_send_query中的错误(conn @ ptr,statement):near",":syntax 错误
答案 0 :(得分:0)
我开始关闭Invoice
列。
df1[-1]%>%group_by(Supplier,OA,Period)%>%
mutate(Spend=sum(Spend))%>%right_join(df2)%>%
subset(!duplicated(.))%>%spread(Period,Spend)%>%
cbind(Tot=rowSums(.[-(1:2)],T))%>%ungroup()
#Joining, by = c("Supplier", "OA")
# A tibble: 4 x 6
Supplier OA P1 P2 P3 Tot
<chr> <chr> <int> <int> <int> <dbl>
1 A 0 500 NA NA 500
2 A A1 400 50 NA 450
3 B 0 NA NA 50 50
4 C C5 65 NA NA 65