Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables

2020-09-09 Thread Aner Perez
Of course, right when I hit send I thought of another option that makes the SQL a little less readable but perhaps gets rid of the ambiguity. Using ordinals in the GROUP BY: SELECT path[1], path[2], path[3], path[4], sum(value) FROM bind_group_by GROUP BY GROUPING SETS ( (1, 2, 3, 4), (1,

Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables

2020-09-09 Thread Tom Lane
Aner Perez writes: > Or if using indexed path elements in the GROUP BY is the issue and I should > put the array indexing in a subselect and do the aggregation in the outer > select. Like this: > -- Safer Subselect Version -- > SELECT level1, level2, level3, level4, sum(value) > FROM ( > SEL

Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables

2020-09-09 Thread Aner Perez
Thanks Tom, I figured as much about the second query but I thought it would be safe to use the first version with the inlined indexing. I'm not sure if you're saying that the same query without the unindexed path column in the select would be safe. Like this: -- Do not GROUP BY or SELECT on path

Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables

2020-09-09 Thread Tom Lane
Aner Perez writes: > [ these queries don't give the same results: ] > SELECT path[1], path[2], path[3], path, sum(value) > FROM bind_group_by > GROUP BY GROUPING SETS ( > (path[1], path[2], path[3], path), > (path[1], path[2], path[3]), > (path[1], path[2]), > (path[1]), > ()