I'm trying to use dplyr against my postgres database and am conducting a simple function. Everything works if I parse the column name directly, however I want to do this dynamically (i.e. sort through each column name from another dataframe
The problem I'm geeting is for the first two calculations, i'm getting the right results
Assume the first dynamic column is called "id"
pull_table %>%
summarise(
row_count = n(),
distinct_count = n_distinct(var) ,
distinct_count_minus_blank = n_distinct(ifelse(var=="",NA,var)),
maxvalue = max(var),
minvalue = min(var),
maxlength = max(length(var)),
minlen = min(length(var))
) %>%
show_query()
The wrong result I get is obvious when you see the sql - sometimes id has '' around it so it's calculating as a string:
<SQL>
SELECT
COUNT(*) AS "row_count",
COUNT(DISTINCT id) AS "distinct_count",
COUNT(
DISTINCT CASE
WHEN ('id' = '') THEN (NULL)
WHEN NOT('id' = '') THEN ('id')
END) AS "distinct_count_minus_blank",
MAX('id') AS "maxvalue",
MIN('id') AS "minvalue",
MAX(LENGTH('id')) AS "maxlength",
MIN(LENGTH('id')) AS "minlen"
FROM "table"
You can see from this output that sometimes the calculation is happening on the column, but sometimes it's just happening on the string "id". Why is this and how can I fix it so it calculates on the actual column rather than the string?