Skip to content

Commit 72655ae

Browse files
Patryk Jatczakpiotrczarnas
Patryk Jatczak
authored andcommitted
Merged PR 3079: fixed group by in table uniqueness for sql server
fixed group by in table uniqueness for sql server Related work items: #13035
2 parents 2c75eb2 + 5bb294d commit 72655ae

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

home/sensors/table/uniqueness/duplicate_record_count/sqlserver.sql.jinja2

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}
22

3-
{% macro extract_in_list(values_list, column_prefix = none, column_suffix = none, separate_by_comma = false) %}
3+
{%- macro extract_in_list(values_list, column_prefix = none, column_suffix = none, separate_by_comma = false) %}
44
{%- set column_names = table.columns if values_list is none or (values_list | length()) == 0 else values_list -%}
55
{%- for item in column_names -%}
66
{{ (column_prefix) if column_prefix is not none -}} {{- lib.quote_identifier(item) -}} {{- (column_suffix) if column_suffix is not none -}} {{- ", " if not loop.last }} {{- "', ', " if separate_by_comma and not loop.last }}
77
{%- endfor -%}
8+
{% endmacro -%}
9+
10+
{% macro render_group_by(table_alias_prefix = 'grouping_table', indentation = ' ') %}
11+
{%- if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none -%}
12+
GROUP BY
13+
{%- endif -%}
14+
{%- if lib.data_groupings is not none and (lib.data_groupings | length()) > 0 -%}
15+
{%- for attribute in lib.data_groupings -%}
16+
{{- ',' if not loop.first -}}{{- lib.eol() }}
17+
{{ indentation }}{{ table_alias_prefix }}.grouping_{{ attribute -}}
18+
{%- endfor -%}
19+
{%- endif -%}
20+
{%- if lib.time_series is not none -%}
21+
{{ ',' if lib.data_groupings is not none and (lib.data_groupings | length()) > 0 -}}{{- lib.eol() -}}
22+
{{ indentation }}time_period,{{ lib.eol() -}}
23+
{{ indentation }}time_period_utc
24+
{%- endif -%}
825
{% endmacro %}
926

1027
SELECT
@@ -20,7 +37,6 @@ FROM (
2037
{{- lib.render_time_dimension_projection('analyzed_table', indentation=' ') }}
2138
FROM {{ lib.render_target_table() }} AS analyzed_table
2239
{{- lib.render_where_clause(indentation=' ', extra_filter = 'COALESCE(' ~ extract_in_list(parameters.columns, column_prefix='CAST(', column_suffix=' AS VARCHAR)') ~ ') IS NOT NULL') }}
23-
GROUP BY {{- extract_in_list(parameters.columns) -}} {{- (", " ~ lib.render_grouping_column_names()) if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none }}
40+
GROUP BY {{ extract_in_list(parameters.columns) -}} {{- (", " ~ lib.render_grouping_column_names()) if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none }}
2441
) grouping_table
25-
{{- lib.render_group_by() -}}
26-
{{- lib.render_order_by() -}}
42+
{{ render_group_by('grouping_table') }}

home/sensors/table/uniqueness/duplicate_record_percent/sqlserver.sql.jinja2

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
{%- endfor -%}
88
{% endmacro -%}
99

10+
{% macro render_group_by(table_alias_prefix = 'grouping_table', indentation = ' ') %}
11+
{%- if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none -%}
12+
GROUP BY
13+
{%- endif -%}
14+
{%- if lib.data_groupings is not none and (lib.data_groupings | length()) > 0 -%}
15+
{%- for attribute in lib.data_groupings -%}
16+
{{- ',' if not loop.first -}}{{- lib.eol() }}
17+
{{ indentation }}{{ table_alias_prefix }}.grouping_{{ attribute -}}
18+
{%- endfor -%}
19+
{%- endif -%}
20+
{%- if lib.time_series is not none -%}
21+
{{ ',' if lib.data_groupings is not none and (lib.data_groupings | length()) > 0 -}}{{- lib.eol() -}}
22+
{{ indentation }}time_period,{{ lib.eol() -}}
23+
{{ indentation }}time_period_utc
24+
{%- endif -%}
25+
{% endmacro %}
26+
1027
SELECT
1128
CASE WHEN SUM(distinct_records) IS NULL THEN 0
1229
ELSE (1 - SUM(distinct_records) * 1.0 / SUM(records_number)) * 100.0 END
@@ -20,7 +37,6 @@ FROM (
2037
{{- lib.render_time_dimension_projection('analyzed_table', indentation=' ') }}
2138
FROM {{ lib.render_target_table() }} AS analyzed_table
2239
{{- lib.render_where_clause(indentation=' ', extra_filter = 'COALESCE(' ~ extract_in_list(parameters.columns, column_prefix='CAST(', column_suffix=' AS VARCHAR)') ~ ') IS NOT NULL') }}
23-
GROUP BY {{- extract_in_list(parameters.columns) -}} {{- (", " ~ lib.render_grouping_column_names()) if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none }}
40+
GROUP BY {{ extract_in_list(parameters.columns) -}} {{- (", " ~ lib.render_grouping_column_names()) if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none }}
2441
) grouping_table
25-
{{- lib.render_group_by() -}}
26-
{{- lib.render_order_by() -}}
42+
{{ render_group_by('grouping_table') }}

0 commit comments

Comments
 (0)