-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more macros. Now we have a helpers folder to modularize functions…
… and an excel folder for the macros that resemble excel formulas.
- Loading branch information
Showing
10 changed files
with
132 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{# | ||
Returns the average (arithmetic mean) of the values in a column. | ||
Handles text and non-numeric values. | ||
When is this a good idea? Not sure. Lol. | ||
|
||
Example: | ||
|
||
Input: `Table` | ||
|
||
| example | | ||
|---------| | ||
| 10 | | ||
| 10s | | ||
| 30 | | ||
| True | | ||
|
||
select | ||
{{ averagea('example') }} | ||
from public.test | ||
|
||
Output: | ||
10.25 | ||
|
||
Arguments: | ||
column: Column name, required | ||
#} | ||
|
||
{% macro averagea(column) %} | ||
|
||
avg({{ reckless_casting(column) }}) | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{# | ||
Calculates the average of values in a range based on a true or false condition. | ||
|
||
Example: | ||
|
||
Input: 'B2:B4', 'Product A', 'A2:A4' | ||
|
||
| Quantity | Product | | ||
|----------|-----------| | ||
| 2 | Product A | | ||
| 4 | Product A | | ||
| 3 | Product B | | ||
|
||
select {{ averageif('B2:B4', 'Product A', 'A2:A4') }} | ||
|
||
Output: | ||
3 | ||
|
||
Arguments: | ||
range: Range of cells that you want evaluated by criteria, required. | ||
criteria: Text that defines which cells will be added, required. | ||
sum_range: The actual cells to aggregate, required. | ||
#} | ||
|
||
{% macro averageif(range, criteria, average_range) %} | ||
( | ||
select avg({{ average_range }}) | ||
from {{ table }} | ||
where range = criteria | ||
) | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{# | ||
Returns the sum of an expression evaluated for each row in a table. | ||
|
||
Example: | ||
|
||
Input: 'Orders', 'Quantity * Sales' | ||
|
||
| Quantity | Sales | | ||
|----------|--------| | ||
| 2 | 10 | | ||
| 1 | 20 | | ||
| 3 | 5 | | ||
|
||
select {{ sumx('Orders', 'Quantity * Sales') }} | ||
|
||
Output: | ||
55 | ||
|
||
Arguments: | ||
table: The table containing the rows for which the expression will be evaluated, required. | ||
expression: The expression to be evaluated for each row of the table, required. | ||
#} | ||
|
||
{% macro sumx(table, expression) %} | ||
( | ||
select sum({{ expression }}) | ||
from {{ table }} | ||
) | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% macro enforce_string(column) %} | ||
|
||
lower(cast({{ column }} as text)) | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{%- macro get_column_names(relation) -%} | ||
|
||
{%- set columns = adapter.get_columns_in_relation(relation) -%} | ||
{%- set columns_list = [] -%} | ||
|
||
{%- for column in columns -%} | ||
{{ columns_list.append(columns_list.name) }} | ||
{%- endfor -%} | ||
{{ return(columns_list) }} | ||
|
||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% macro letters_to_numbers(letter) %} | ||
|
||
ascii(upper(letter)) - 64 | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% macro reckless_casting(column) %} | ||
|
||
{% set match_positive_boolean = ('y', 'yes', 't', 'true') %} | ||
{% set match_negative_boolean = ('n', 'no', 'f', 'false') %} | ||
{% set integer_regex = '^([0-9]+)$' %} | ||
{% set float_regex = '^[-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?$' %} | ||
{% set text_regex = '[^0-9.]' %} | ||
|
||
case | ||
when {{ enforce_string(column) }} in {{ match_positive_boolean }} then 1.0 | ||
when {{ enforce_string(column) }} in {{ match_negative_boolean }} then 0.0 | ||
when regexp_matches({{ enforce_string(column) }}, '{{ integer_regex }}') then cast({{ column }} as real) | ||
when regexp_matches({{ enforce_string(column) }}, '{{ float_regex }}') then cast({{ column }} as real) | ||
when regexp_matches({{ enforce_string(column) }}, '{{ text_regex }}') then 0.0 | ||
end | ||
|
||
{% endmacro %} |
File renamed without changes.