# get\_column\_values not unique?

**URL:** <https://discourse.getdbt.com/t/get-column-values-not-unique/19810>\
**Category:** Help\
**Tags:** jinja\
**Created:** [July 9, 2025, 9:08am UTC](https://discourse.getdbt.com/t/get-column-values-not-unique/19810 "2025-07-09T09:08:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricky\_m](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@ricky\_m](https://discourse.getdbt.com/u/ricky_m)\
**Post date:** [July 9, 2025, 9:08am UTC](https://discourse.getdbt.com/t/get-column-values-not-unique/19810/1 "2025-07-09T09:08:14Z")

</div>

## The problem

I want to use the values of a column to produce an SQL query, via UNIONs - I have a list of relevant values from `dbt_utils.get_column_values()` for this, but this function only returns unique values and therefore it won’t work for my use case.

## The context of why I’m trying to do this

I’m creating an audit table for all models in a schema, and the values in each row refer to column names for the most part. These column names can vary between tables. Here are some examples:

| table\_name | monitor\_flag | created\_col | modified\_col | ingested\_col | dbt\_runtime\_col | expected\_max\_delay\_minutes |
| --- | --- | --- | --- | --- | --- | --- |
| lead | true | created\_at | modified\_at | loaded\_at | dbt\_utc\_runtime | 1440 |
| owner | true | created\_at | modified\_at | loaded\_at | dbt\_utc\_runtime | 1440 |
| customer | true | src\_created\_at | src\_modified\_at | loaded\_at | dbt\_utc\_runtime | 1440 |

Ultimately this will query each table for the latest freshness stats on a daily basis, something like this:

```auto
select
      '{{ table }}' as table_name,
      current_date as report_date,
      count(*) as total_row_count,
      sum(case when date({{ created_col }}) = current_date then 1 else 0 end) as created_today,
      {% if modified_col != '' %}
        sum(case when date({{ modified_col }}) = current_date and {{ modified_col }} != {{ created_col }} then 1 else 0 end) as modified_only_today,
      {% else %}
        0 as modified_only_today,
      {% endif %}
      max({{ ingested_col }}) as max_ingested_at,
      max({{ created_col }}) as max_created_at,
      {% if modified_col != '' %}
        max({{ modified_col }}) as max_modified_at,
      {% else %}
        null as max_modified_at,
      {% endif %}
    from {{ table }}

```

However for now I would be happy to see all values in a column.

## What I’ve already tried

```auto
with config as (
    select * from {{ ref('monitoring__audit_config') }}
)

{% set tables = dbt_utils.get_column_values(table=ref('monitoring__audit_config'), column='table_name') %}
{% set created_cols = dbt_utils.get_column_values(table=ref('monitoring__audit_config'), column='created_col') %}
{% set modified_cols = dbt_utils.get_column_values(table=ref('monitoring__audit_config'), column='modified_col') %}

{% for item in modified_cols %}
    {% do(log(item, info=True))%}
{% endfor %}

select * from config

```

Currently, do(log…) outputs `modified_at` , `src_modified_at`. If I can get it to output `modified_at` , `modified_at` , `src_modified_at` then I’m confident the whole thing will work.

Can anyone help?

---

<div class="post-metadata">

**Author:** ![mehdiatty21](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/mehdiatty21/32/5831_2.png) [@mehdiatty21](https://discourse.getdbt.com/u/mehdiatty21)\
**Post date:** [July 9, 2025, 1:23pm UTC](https://discourse.getdbt.com/t/get-column-values-not-unique/19810/2 "2025-07-09T13:23:20Z")

</div>

Hi Ricky,  
Thanks for sharing your use case.  
You are right dbt\_utils.get\_column\_values() uses select disctinct,so it will always return unique values without preserving duplicates.  
Therefore,if you have to retrieve non-distinct values from a certain column,you have to use a custom macro,you can use this example and adapt it:

```auto
{%macro get_all_column_values(model, column)%}
      {% set query %}
                    select {{ column }} from {{ model }}
       {% endset %}
       {% set results = run_query(query) %}
       {% if execute %}
               {% set values = results.columns[0].values() %}
       {% else %}
               {% set values = [] %}
       {% endif %}
        
       {{ return(values) }}
{% endmacro %}

```

Then call the macro as follow :  
`{% set modified_cols = get_all_column_values(ref('monitoring_audit_config'), 'modified_col') %}`  
this will give you a full list,including duplicates.  
Let me know if you’d like help adapting it further.I will be happy to assist you!

---

<div class="post-metadata">

**Author:** ![ricky\_m](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@ricky\_m](https://discourse.getdbt.com/u/ricky_m)\
**Post date:** [July 10, 2025, 10:58am UTC](https://discourse.getdbt.com/t/get-column-values-not-unique/19810/3 "2025-07-10T10:58:45Z")

</div>

Many thanks @mehdiatty21 that works perfectly!

I’d started working on much the same lines (didn’t think someone would reply so quickly), but your solution is better than mine so much obliged.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [July 17, 2025, 10:59am UTC](https://discourse.getdbt.com/t/get-column-values-not-unique/19810/4 "2025-07-17T10:59:27Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
