Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent results given by array_agg vs group_by #15238

Open
jadami10 opened this issue Mar 11, 2025 · 0 comments
Open

Inconsistent results given by array_agg vs group_by #15238

jadami10 opened this issue Mar 11, 2025 · 0 comments

Comments

@jadami10
Copy link
Contributor

This is a simplified version of a user query

SELECT 
  start_month,
  end_month,
  array_agg(segment_category, 'STRING', true) AS segment_category,
  distinctcount(segment_category) AS distinctcount
FROM metrics_table
WHERE subset_name = 'All Data'
  AND start_month = '2024-01-01'
  AND end_month = '2024-09-01'
GROUP BY 1, 2
UNION ALL
SELECT 
  'no group by' AS start_month,
  'no group by' AS end_month,
  array_agg(segment_category, 'STRING', true) AS segment_category,
  distinctcount(segment_category) AS distinctcount
FROM metrics_table
WHERE subset_name = 'All Data'
  AND start_month = '2024-01-01'
  AND end_month = '2024-09-01'

What we see here is

start_month end_month segment_category distinctcount
2024-01-01 2024-09-01 segment_a,segment_b,segment_c,segment_d,segment_e,segment_f,segment_g,segment_h 8
no group by no group by segment_a,segment_b,segment_c,segment_d,segment_f,segment_g,segment_h 8

without the group by, we still get the right distinct count, but one of the groups is missing in array_agg. This happens consistently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant