I have a query (a view actually) the normally should define a unique mapping from key
to value2
.
select key, value1, value2
from tbl -- joins are left out here
where (1=1) -- left out here
order by key
;
and usually the result is something like
key value1 value2
--------------------------
Harry fish blue
Sally dog green
Willy dog red
so I can map key
to value2
uniquely.
But as it happens the underlying data may contain junk some day:
key value1 value2
--------------------------
Harry fish blue
Harry fish black -- <<< breaks uniqueness
Sally dog green
Willy dog red
I would like to add HAVING
and/or GROUP BY
expressions to identify the nun-unique rows easily. Because I have to do something with value2
in this case I have to aggregate it somehow -- my idea is max
.
Therefore an example result would be
key value1 value2 count_value2
----------------------------------------
Harry fish blue 2
Sally dog green 1
Willy dog red 1
But as always, I am completely baffled by GROUP BY
and its consorts. Where do I but it? Where and where the aggregations?