Skip to content

Commit

Permalink
updated make aggregration sql function
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrohagan committed Aug 18, 2024
1 parent 46a0751 commit 14823ad
Show file tree
Hide file tree
Showing 20 changed files with 1,083 additions and 32,923 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(abc_graph)
export(clean_file_names)
export(count_plus)
export(divide)
export(make_aggregation_sql)
export(make_aggregation_tbl)
export(make_cohort_tbl)
export(make_segmentation)
Expand Down
56 changes: 1 addition & 55 deletions R/time_intelligence.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,68 +68,14 @@ make_aggregation_tbl <- function(.data,...,date_var,value_var, time_unit) {
)



return(full_tbl)

}




make_aggregation_sql <- function(.data,...,date_var,value_var, time_unit){

cols <- enquos(...)
group_cols <- deparse(cols)


summary_sql <- glue_sql("
WITH summary_tbl AS (
SELECT
DATE_TRUNC({time_unit}, {`date_var`}) AS date,
{`value_var`},
{`glue_sql_collapse(group_vars, sep = ', ')`}
FROM {`DBI::SQL(.data)`} -- Replace .data with your actual table
GROUP BY date, {`glue_sql_collapse(group_vars, sep = ', ')`}
),
calendar_tbl AS (
SELECT
generate_series(
DATE_TRUNC({time_unit}, MIN(date)),
DATE_TRUNC({time_unit}, MAX(date)),
INTERVAL '1 {time_unit}'
)::DATE AS date
FROM summary_tbl
),
crossing_tbl AS (
SELECT
calendar_tbl.date,
{`glue_sql_collapse(group_vars, sep = ', ')`}
FROM
calendar_tbl
CROSS JOIN (
SELECT DISTINCT {`glue_sql_collapse(group_vars, sep = ', ')`}
FROM summary_tbl
) AS distinct_groups
),
full_tbl AS (
SELECT
crossing_tbl.date,
{`glue_sql_collapse(group_vars, sep = ', ')'},
COALESCE(summary_tbl.{`value_var`}, 0) AS {`value_var`}
FROM
crossing_tbl
LEFT JOIN summary_tbl
ON crossing_tbl.date = summary_tbl.date
AND {`paste(glue_sql('{distinct_groups}', .con = DBI::SQL('')), collapse = ' AND ')`}
)
SELECT * FROM full_tbl
", .con = DBI::SQL("your_database_connection"))

}





Expand Down
13 changes: 9 additions & 4 deletions README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ format: gfm

- Cleaned up group by logic in time intelligence functions

## to do list

## 2024-08-13
- translated make_aggregation_tbl to sql (without group logic)
- fixed how to add sub query that contains all previous steps
## 2024-08-15

## to do list
- Figure out how to delay unquoted evaluation so that we can pass args to make aggregtation tbl
- replicate time intellitgence functions
- update examples with contoso exmaple
- convert time intelligence to sql equivalents
- covert lm to sql equivalents
- design and create robust unit tests
- make 554 calendar date table
- create documentation with examples

- investigate factor anlaysis

# Introduction to fpaR

Expand Down Expand Up @@ -145,7 +151,6 @@ tibble::tribble(
)
```

Like-for-like comparison

factor analysis

Expand Down
13 changes: 0 additions & 13 deletions derby.log

This file was deleted.

Loading

0 comments on commit 14823ad

Please sign in to comment.