Scenario 1:
Table:
id column1 column2
1 "bla" "foo"
2 "bla" "bar"
I want to group by column1 and get null for column2, cause there’s not the same value in all rows.
Scenario 2:
Table:
id column1 column2
1 "bla" "foo"
2 "bla" "foo"
I want to group by column1 and get foo for column2, cause all values of column2 are equal.
Is it possible to solve this by a sql statement?
Since you want to group by
column1, one way to know ifcolumn2has all same values is to check ifmax(column2) = min(column2), therefore this should work:Edit
If your
column2cannot acceptnullvalues, then the above query is ok, otherwise you need the following one to handle cases whencolumn2isnull:The only difference is that we need to add a
casecondition to cover the case of whencolumn2 is null