sum(distinct)?

Wow ,  I learned something new about SQL today.  Apparently, you can now use the “distinct” keyword inside sum().  Like this;

mysql> create table foo (a int);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into foo (a) values (1);
Query OK, 1 row affected (0.11 sec)
mysql> insert into foo (a) values (1);
Query OK, 1 row affected (0.02 sec)
mysql> select sum(distinct a)
    -> from foo;
+-----------------+
| sum(distinct a) |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.02 sec)

I’m not sure how this is useful, but it’s interesting.

Leave a Reply