I have the following table in which I am trying to use SUM if type = 'printer', however I would not want to count the repetition of client_ids. So I expect something like this:
+------+-----------+-----------+ | k_id | client_id | type | +------+-----------+-----------+ | 1 | 100 | pc | | 2 | 101 | printer | | 3 | 101 | printer | | 4 | 101 | printer | | 5 | 102 | cellphone | +------+-----------+-----------+
Query:
SELECT client_id, SUM(IF(type = 'printer', 1,0)) FROM FOO GROUP BY type, client_id;
Result:
+-----------+--------------------------------+ | client_id | SUM(IF(type = 'printer', 1,0)) | +-----------+--------------------------------+ | 102 | 0 | | 100 | 0 | | 101 | 3 | +-----------+--------------------------------+
Expected Result:
+-----------+--------------------------------+ | client_id | SUM(IF(type = 'printer', 1,0)) | +-----------+--------------------------------+ | 102 | 0 | | 100 | 0 | | 101 | 1 | +-----------+--------------------------------+
Josh
source share