I have a sql table with following values
| col1 | col2| source | values
| 1 | 2 | A | null
| 1 | 2 | B | 1.0
| 1 | 2 | C | null
| 1 | 4 | A | 2.0
| 1 | 4 | B | 2.0
| 1 | 4 | C | 2.0
| 1 | 5 | A | null
| 1 | 5 | B | null
| 1 | 5 | C | null
How can I get an output with a group by of col1 and col2 with a flag:
- all values match for a group ( flag = 1)
- all values are null ( flag = 2)
- some values is null (flag = 3)
Output:
| col1 | col2| flag
| 1 | 2 | 3
| 1 | 4 | 1
| 1 | 5 | 2
Or: based on your updated question:SQL Fiddle Demo
This will give you:
Note that: I assumed that the flag is how many
NULLvalues are in theVALUEScolumn, so I used"Values" IS NULLinstead ofSomeConditionHere.I couldn’t understand how the
flagshould be computed in the expected results you posted. You have to use the predicate that define your flag instead of"Values" IS NULL.Update:
Try this:
Updated SQL Fiddle Demo
This will give you: