Skip to content

Commit

Permalink
rafa tests exploring weighted_avg
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Dec 18, 2024
1 parent 98fb7e6 commit 61df6bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/tests_rafa/tests_arrow_vs_duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ df_duck_dani <- geocodebr:::geocode2(
addresses_table = input_df,
address_fields = fields,
n_cores = 7,
progress = T )
progress = T
)
tictoc::toc()
# 900K: 13 secs

Expand Down
32 changes: 32 additions & 0 deletions tests/tests_rafa/weighted_avg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
I have two tables:

tableA looks like this:

id | number
1 | 100
2 | 120
3 | 300

table B looks like this:
number | longitude
100 | -41.1
100 | -41.101
150 | -41.3
300 | -41.2

Write the code in SQL with duckdb to perform left join between tables A and B based on column 'id' so that the logitude of an 'id' will be the average of longitudes in table B weighted by the difference between the numbers in table A and table B




"SELECT
A.id,
SUM((1/ABS(A.number - B.number) * B.longitude)) / SUM(1/ABS(A.number - B.number)) AS weighted_avg_longitude
FROM
tableA AS A
LEFT JOIN
tableB AS B
ON
A.number = B.number
GROUP BY
A.id;"

0 comments on commit 61df6bf

Please sign in to comment.