sql - Example for GROUP BY restriction -
in postgresql documentation, says:
when group present, not valid select list expressions refer ungrouped columns except within aggregate functions or if ungrouped column functionally dependent on grouped columns, since there otherwise more 1 possible value return ungrouped column
i don't understand part "there otherwise more 1 possible value return ungrouped column".
can please give me example of this? how can there more 1 possible value return ungroup column?
consider table:
col_1 | col_2 | col_3 10 10 11 20 b 20 40 c 40 60 and try running query sums on values of col_3
select col_1, col_2, sum(col_3) t1 group col_1 the above query can have 2 possible outputs:
output 1: here, col_2 = 10 --------------------------- col_1 | col_2 | sum(col_3) 10 30 b 20 40 c 40 60 output 2: here, col_2 = 11 --------------------------- col_1 | col_2 | sum(col_3) 11 30 b 20 40 c 40 60 and because col_2 not included in group clause. summation on col_3 happens expected based on grouping col_1 sql engine not know whether wanted row having col_2 11 or 11. , hence, "there otherwise more 1 possible value return ungrouped column"
the above query work fine mysql, randomly return 1 of above 2 outputs whereas oracle/sql server throw error mentioned
Comments
Post a Comment