Skip to content

expected numbers in use count

expected numbers in use count checks

Description
Column level check that counts unique values in a numeric column and counts how many values out of a list of expected numeric values were found in the column. The check raises a data quality issue when the threshold of maximum number of missing values was exceeded (too many expected values were not found in the column). This check is useful for analysing columns with a low number of unique values, such as status codes, to detect that all status codes are in use in any row.


profile expected numbers in use count

Check description
Verifies that the expected numeric values were found in the column. Raises a data quality issue when too many expected values were not found (were missing).

Check name Check type Time scale Sensor definition Quality rule
profile_expected_numbers_in_use_count profiling expected_numbers_in_use_count max_missing

Enable check (Shell)
To enable this check provide connection name and check name in check enable command

dqo> check enable -c=connection_name -ch=profile_expected_numbers_in_use_count
Run check (Shell)
To run this check provide check name in check run command
dqo> check run -ch=profile_expected_numbers_in_use_count
It is also possible to run this check on a specific connection. In order to do this, add the connection name to the below
dqo> check run -c=connection_name -ch=profile_expected_numbers_in_use_count
It is additionally feasible to run this check on a specific table. In order to do this, add the table name to the below
dqo> check run -c=connection_name -t=table_name -ch=profile_expected_numbers_in_use_count
It is furthermore viable to combine run this check on a specific column. In order to do this, add the column name to the below
dqo> check run -c=connection_name -t=table_name -col=column_name -ch=profile_expected_numbers_in_use_count
Check structure (Yaml)
      profiling_checks:
        numeric:
          profile_expected_numbers_in_use_count:
            parameters:
              expected_values:
              - 2
              - 3
            warning:
              max_missing: 1
            error:
              max_missing: 1
            fatal:
              max_missing: 2
Sample configuration (Yaml)
# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  columns:
    target_column:
      profiling_checks:
        numeric:
          profile_expected_numbers_in_use_count:
            parameters:
              expected_values:
              - 2
              - 3
            warning:
              max_missing: 1
            error:
              max_missing: 1
            fatal:
              max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested

BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH) AS time_period,
    TIMESTAMP(DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS time_period,
    CAST(TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date)) AS time_period,
    TO_TIMESTAMP(DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date))) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0) AS time_period,
    CAST((DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table

Configuration with data grouping

Click to see more

Sample configuration (Yaml)

# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  default_grouping_name: group_by_country_and_state
  groupings:
    group_by_country_and_state:
      level_1:
        source: column_value
        column: country
      level_2:
        source: column_value
        column: state
  columns:
    target_column:
      profiling_checks:
        numeric:
          profile_expected_numbers_in_use_count:
            parameters:
              expected_values:
              - 2
              - 3
            warning:
              max_missing: 1
            error:
              max_missing: 1
            fatal:
              max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested
    country:
      labels:
      - column used as the first grouping key
    state:
      labels:
      - column used as the second grouping key
BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH) AS time_period,
    TIMESTAMP(DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,

                analyzed_table.grouping_level_1,

                analyzed_table.grouping_level_2
,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    original_table."country" AS grouping_level_1,
    original_table."state" AS grouping_level_2,
    TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS time_period,
    CAST(TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date)) AS time_period,
    TO_TIMESTAMP(DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date))) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.[country] AS grouping_level_1,
    analyzed_table.[state] AS grouping_level_2,
    DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0) AS time_period,
    CAST((DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table
GROUP BY analyzed_table.[country], analyzed_table.[state]
ORDER BY level_1, level_2
        , 

daily expected numbers in use count

Check description
Verifies that the expected numeric values were found in the column. Raises a data quality issue when too many expected values were not found (were missing). Stores the most recent captured value for each day when the data quality check was evaluated.

Check name Check type Time scale Sensor definition Quality rule
daily_expected_numbers_in_use_count recurring daily expected_numbers_in_use_count max_missing

Enable check (Shell)
To enable this check provide connection name and check name in check enable command

dqo> check enable -c=connection_name -ch=daily_expected_numbers_in_use_count
Run check (Shell)
To run this check provide check name in check run command
dqo> check run -ch=daily_expected_numbers_in_use_count
It is also possible to run this check on a specific connection. In order to do this, add the connection name to the below
dqo> check run -c=connection_name -ch=daily_expected_numbers_in_use_count
It is additionally feasible to run this check on a specific table. In order to do this, add the table name to the below
dqo> check run -c=connection_name -t=table_name -ch=daily_expected_numbers_in_use_count
It is furthermore viable to combine run this check on a specific column. In order to do this, add the column name to the below
dqo> check run -c=connection_name -t=table_name -col=column_name -ch=daily_expected_numbers_in_use_count
Check structure (Yaml)
      recurring_checks:
        daily:
          numeric:
            daily_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
Sample configuration (Yaml)
# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  columns:
    target_column:
      recurring_checks:
        daily:
          numeric:
            daily_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested

BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    CAST(CURRENT_TIMESTAMP() AS DATE) AS time_period,
    TIMESTAMP(CAST(CURRENT_TIMESTAMP() AS DATE)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-%d 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-%d 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    TRUNC(CAST(CURRENT_TIMESTAMP AS DATE)) AS time_period,
    CAST(TRUNC(CAST(CURRENT_TIMESTAMP AS DATE)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(LOCALTIMESTAMP AS date) AS time_period,
    CAST((CAST(LOCALTIMESTAMP AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(LOCALTIMESTAMP AS date) AS time_period,
    CAST((CAST(LOCALTIMESTAMP AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date) AS time_period,
    TO_TIMESTAMP(CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date)) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(SYSDATETIMEOFFSET() AS date) AS time_period,
    CAST((CAST(SYSDATETIMEOFFSET() AS date)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table

Configuration with data grouping

Click to see more

Sample configuration (Yaml)

# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  default_grouping_name: group_by_country_and_state
  groupings:
    group_by_country_and_state:
      level_1:
        source: column_value
        column: country
      level_2:
        source: column_value
        column: state
  columns:
    target_column:
      recurring_checks:
        daily:
          numeric:
            daily_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested
    country:
      labels:
      - column used as the first grouping key
    state:
      labels:
      - column used as the second grouping key
BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    CAST(CURRENT_TIMESTAMP() AS DATE) AS time_period,
    TIMESTAMP(CAST(CURRENT_TIMESTAMP() AS DATE)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-%d 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-%d 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,

                analyzed_table.grouping_level_1,

                analyzed_table.grouping_level_2
,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    original_table."country" AS grouping_level_1,
    original_table."state" AS grouping_level_2,
    TRUNC(CAST(CURRENT_TIMESTAMP AS DATE)) AS time_period,
    CAST(TRUNC(CAST(CURRENT_TIMESTAMP AS DATE)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    CAST(LOCALTIMESTAMP AS date) AS time_period,
    CAST((CAST(LOCALTIMESTAMP AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    CAST(LOCALTIMESTAMP AS date) AS time_period,
    CAST((CAST(LOCALTIMESTAMP AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date) AS time_period,
    TO_TIMESTAMP(CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date)) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.[country] AS grouping_level_1,
    analyzed_table.[state] AS grouping_level_2,
    CAST(SYSDATETIMEOFFSET() AS date) AS time_period,
    CAST((CAST(SYSDATETIMEOFFSET() AS date)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table
GROUP BY analyzed_table.[country], analyzed_table.[state]
ORDER BY level_1, level_2
        , 

monthly expected numbers in use count

Check description
Verifies that the expected numeric values were found in the column. Raises a data quality issue when too many expected values were not found (were missing). Stores the most recent row count for each month when the data quality check was evaluated.

Check name Check type Time scale Sensor definition Quality rule
monthly_expected_numbers_in_use_count recurring monthly expected_numbers_in_use_count max_missing

Enable check (Shell)
To enable this check provide connection name and check name in check enable command

dqo> check enable -c=connection_name -ch=monthly_expected_numbers_in_use_count
Run check (Shell)
To run this check provide check name in check run command
dqo> check run -ch=monthly_expected_numbers_in_use_count
It is also possible to run this check on a specific connection. In order to do this, add the connection name to the below
dqo> check run -c=connection_name -ch=monthly_expected_numbers_in_use_count
It is additionally feasible to run this check on a specific table. In order to do this, add the table name to the below
dqo> check run -c=connection_name -t=table_name -ch=monthly_expected_numbers_in_use_count
It is furthermore viable to combine run this check on a specific column. In order to do this, add the column name to the below
dqo> check run -c=connection_name -t=table_name -col=column_name -ch=monthly_expected_numbers_in_use_count
Check structure (Yaml)
      recurring_checks:
        monthly:
          numeric:
            monthly_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
Sample configuration (Yaml)
# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  columns:
    target_column:
      recurring_checks:
        monthly:
          numeric:
            monthly_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested

BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH) AS time_period,
    TIMESTAMP(DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS time_period,
    CAST(TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date)) AS time_period,
    TO_TIMESTAMP(DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date))) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0) AS time_period,
    CAST((DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table

Configuration with data grouping

Click to see more

Sample configuration (Yaml)

# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  default_grouping_name: group_by_country_and_state
  groupings:
    group_by_country_and_state:
      level_1:
        source: column_value
        column: country
      level_2:
        source: column_value
        column: state
  columns:
    target_column:
      recurring_checks:
        monthly:
          numeric:
            monthly_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested
    country:
      labels:
      - column used as the first grouping key
    state:
      labels:
      - column used as the second grouping key
BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH) AS time_period,
    TIMESTAMP(DATE_TRUNC(CAST(CURRENT_TIMESTAMP() AS DATE), MONTH)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(LOCALTIMESTAMP, '%Y-%m-01 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,

                analyzed_table.grouping_level_1,

                analyzed_table.grouping_level_2
,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    original_table."country" AS grouping_level_1,
    original_table."state" AS grouping_level_2,
    TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS time_period,
    CAST(TRUNC(CAST(CURRENT_TIMESTAMP AS DATE), 'MONTH') AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date)) AS time_period,
    CAST((DATE_TRUNC('MONTH', CAST(LOCALTIMESTAMP AS date))) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date)) AS time_period,
    TO_TIMESTAMP(DATE_TRUNC('MONTH', CAST(TO_TIMESTAMP_NTZ(LOCALTIMESTAMP()) AS date))) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.[country] AS grouping_level_1,
    analyzed_table.[state] AS grouping_level_2,
    DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0) AS time_period,
    CAST((DATEADD(month, DATEDIFF(month, 0, SYSDATETIMEOFFSET()), 0)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table
GROUP BY analyzed_table.[country], analyzed_table.[state]
ORDER BY level_1, level_2
        , 

daily partition expected numbers in use count

Check description
Verifies that the expected numeric values were found in the column. Raises a data quality issue when too many expected values were not found (were missing). Creates a separate data quality check (and an alert) for each daily partition.

Check name Check type Time scale Sensor definition Quality rule
daily_partition_expected_numbers_in_use_count partitioned daily expected_numbers_in_use_count max_missing

Enable check (Shell)
To enable this check provide connection name and check name in check enable command

dqo> check enable -c=connection_name -ch=daily_partition_expected_numbers_in_use_count
Run check (Shell)
To run this check provide check name in check run command
dqo> check run -ch=daily_partition_expected_numbers_in_use_count
It is also possible to run this check on a specific connection. In order to do this, add the connection name to the below
dqo> check run -c=connection_name -ch=daily_partition_expected_numbers_in_use_count
It is additionally feasible to run this check on a specific table. In order to do this, add the table name to the below
dqo> check run -c=connection_name -t=table_name -ch=daily_partition_expected_numbers_in_use_count
It is furthermore viable to combine run this check on a specific column. In order to do this, add the column name to the below
dqo> check run -c=connection_name -t=table_name -col=column_name -ch=daily_partition_expected_numbers_in_use_count
Check structure (Yaml)
      partitioned_checks:
        daily:
          numeric:
            daily_partition_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
Sample configuration (Yaml)
# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  columns:
    target_column:
      partitioned_checks:
        daily:
          numeric:
            daily_partition_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested

BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    CAST(analyzed_table.`` AS DATE) AS time_period,
    TIMESTAMP(CAST(analyzed_table.`` AS DATE)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    DATE_FORMAT(analyzed_table.``, '%Y-%m-%d 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(analyzed_table.``, '%Y-%m-%d 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    TRUNC(CAST(original_table."" AS DATE)) AS time_period,
    CAST(TRUNC(CAST(original_table."" AS DATE)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(analyzed_table."" AS date) AS time_period,
    CAST((CAST(analyzed_table."" AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(analyzed_table."" AS date) AS time_period,
    CAST((CAST(analyzed_table."" AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(analyzed_table."" AS date) AS time_period,
    TO_TIMESTAMP(CAST(analyzed_table."" AS date)) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY time_period, time_period_utc
ORDER BY time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE COUNT_BIG(DISTINCT
        CASE
            WHEN analyzed_table.[target_column] IN (2, 3
)
                THEN analyzed_table.[target_column]
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    CAST(analyzed_table.[] AS date) AS time_period,
    CAST((CAST(analyzed_table.[] AS date)) AS DATETIME) AS time_period_utc
FROM [your_sql_server_database].[<target_schema>].[<target_table>] AS analyzed_table
GROUP BY CAST(analyzed_table.[] AS date), CAST(analyzed_table.[] AS date)
ORDER BY CAST(analyzed_table.[] AS date)

Configuration with data grouping

Click to see more

Sample configuration (Yaml)

# yaml-language-server: $schema=https://cloud.dqo.ai/dqo-yaml-schema/TableYaml-schema.json
apiVersion: dqo/v1
kind: table
spec:
  timestamp_columns:
    event_timestamp_column: col_event_timestamp
    ingestion_timestamp_column: col_inserted_at
  incremental_time_window:
    daily_partitioning_recent_days: 7
    monthly_partitioning_recent_months: 1
  default_grouping_name: group_by_country_and_state
  groupings:
    group_by_country_and_state:
      level_1:
        source: column_value
        column: country
      level_2:
        source: column_value
        column: state
  columns:
    target_column:
      partitioned_checks:
        daily:
          numeric:
            daily_partition_expected_numbers_in_use_count:
              parameters:
                expected_values:
                - 2
                - 3
              warning:
                max_missing: 1
              error:
                max_missing: 1
              fatal:
                max_missing: 2
      labels:
      - This is the column that is analyzed for data quality issues
    col_event_timestamp:
      labels:
      - optional column that stores the timestamp when the event/transaction happened
    col_inserted_at:
      labels:
      - optional column that stores the timestamp when row was ingested
    country:
      labels:
      - column used as the first grouping key
    state:
      labels:
      - column used as the second grouping key
BigQuery

{% import '/dialects/bigquery.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    CAST(analyzed_table.`` AS DATE) AS time_period,
    TIMESTAMP(CAST(analyzed_table.`` AS DATE)) AS time_period_utc
FROM `your-google-project-id`.`<target_schema>`.`<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

MySQL

{% import '/dialects/mysql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table.`target_column` IN (2, 3
)
                THEN analyzed_table.`target_column`
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table.`country` AS grouping_level_1,
    analyzed_table.`state` AS grouping_level_2,
    DATE_FORMAT(analyzed_table.``, '%Y-%m-%d 00:00:00') AS time_period,
    FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(analyzed_table.``, '%Y-%m-%d 00:00:00'))) AS time_period_utc
FROM `<target_table>` AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Oracle

{% import '/dialects/oracle.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{ values_list|join(', ') -}}
{% endmacro %}

{%- macro actual_value() -%}
    {%- if 'expected_values' not in parameters or parameters.expected_values | length == 0 -%}
    0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    {{ actual_value() }} AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
 {{- lib.render_data_grouping_projections_reference('analyzed_table') }}
    {{- lib.render_time_dimension_projection_reference('analyzed_table') }}
FROM(
    SELECT
        original_table.*
        {{- lib.render_data_grouping_projections('original_table') }}
        {{- lib.render_time_dimension_projection('original_table') }}
    FROM {{ lib.render_target_table() }} original_table
    {{- lib.render_where_clause(table_alias_prefix='original_table') }}
) analyzed_table
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    ) AS actual_value,
    MAX(2) AS expected_value,

                analyzed_table.grouping_level_1,

                analyzed_table.grouping_level_2
,
    time_period,
    time_period_utc
FROM(
    SELECT
        original_table.*,
    original_table."country" AS grouping_level_1,
    original_table."state" AS grouping_level_2,
    TRUNC(CAST(original_table."" AS DATE)) AS time_period,
    CAST(TRUNC(CAST(original_table."" AS DATE)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
    FROM "<target_schema>"."<target_table>" original_table
) analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

PostgreSQL

{% import '/dialects/postgresql.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    CAST(analyzed_table."" AS date) AS time_period,
    CAST((CAST(analyzed_table."" AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_postgresql_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Redshift

{% import '/dialects/redshift.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    CAST(analyzed_table."" AS date) AS time_period,
    CAST((CAST(analyzed_table."" AS date)) AS TIMESTAMP WITH TIME ZONE) AS time_period_utc
FROM "your_redshift_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

Snowflake

{% import '/dialects/snowflake.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN NULL
        ELSE COUNT(DISTINCT
        CASE
            WHEN analyzed_table."target_column" IN (2, 3
)
                THEN analyzed_table."target_column"
            ELSE NULL
        END
    )
    END AS actual_value,
    MAX(2) AS expected_value,
    analyzed_table."country" AS grouping_level_1,
    analyzed_table."state" AS grouping_level_2,
    CAST(analyzed_table."" AS date) AS time_period,
    TO_TIMESTAMP(CAST(analyzed_table."" AS date)) AS time_period_utc
FROM "your_snowflake_database"."<target_schema>"."<target_table>" AS analyzed_table
GROUP BY grouping_level_1, grouping_level_2, time_period, time_period_utc
ORDER BY grouping_level_1, grouping_level_2, time_period, time_period_utc

SQL Server

{% import '/dialects/sqlserver.sql.jinja2' as lib with context -%}

{%- macro extract_in_list(values_list) -%}
    {{values_list|join(', ')}}
{% endmacro %}

{%- macro render_else() -%}
    {%- if parameters.expected_values|length == 0 -%}
        0
    {%- else -%}
    COUNT_BIG(DISTINCT
        CASE
            WHEN {{ lib.render_target_column('analyzed_table') }} IN ({{ extract_in_list(parameters.expected_values) }})
                THEN {{ lib.render_target_column('analyzed_table') }}
            ELSE NULL
        END
    )
    {%- endif -%}
{% endmacro -%}

SELECT
    CASE
        WHEN COUNT_BIG(*) = 0 THEN MAX(0)
        ELSE {{render_else()}}
    END AS actual_value,
    MAX({{ parameters.expected_values | length }}) AS expected_value
    {{- lib.render_data_grouping_projections('analyzed_table') }}
    {{- lib.render_time_dimension_projection('analyzed_table') }}
FROM {{ lib.render_target_table() }} AS analyzed_table
{{- lib.render_where_clause() -}}
{{- lib.render_group_by() -}}
{{- lib.render_order_by() -}}