adjective
1 recognizably different in nature from something else of a similar type • physically separate
2 readily distinguishable by the senses
—-
In the course of trying to answer a question for my wife this evening, I ran across an interesting type of query that, though it would seem simple and common, I am fairly certain I haven’t ever written in my entire career.
The essence of the problem is “how do you perform a grouping query while simultaneous removing duplicates.”
So, for example, if you had the following data set:
| A | B | C |
|---|---|---|
| a | b | c |
| a | b | c |
| a | d | e |
If you were to do the standard GROUP BY
select a, b, count(c) from test group by a, b order by a, b;
against this table, you would get the following:
a b count ---- ---- -------- a b 2 a d 1
However, my wife didn’t want that duplicate “c” in column “C” counted. Here’s the query I used (in PostgreSQL) to achieve that:
select a, b, count(b) from (select distinct a, b, c from test) AS t group by a, b order by a, b;
resulting in:
a b count ---- ---- -------- a b 1 a d 1
QED
In retrospect, the reason this probably never came up is because with good data, you really shouldn’t have duplicate rows, but you don’t always control your input data.
