Answer the question
In order to leave comments, you need to log in
SQL query to count unique values?
There is a database with camp, random_id, action records. camp can be repeated, as can random_id. action takes only two values 0\1. We need to extract the number of unique random_ids for each of the action values (0/1) and their corresponding camp. Group the output by camp.
Which I just haven't tried. You can simply calculate the unique random_id for each of the action values separately with the query
SELECT camp, COUNT(DISTINCT random_id) FROM table WHERE action = 0 group by camp
SELECT camp, SUM(IF(action = '0', 1, 0)) as uniq_null, SUM(IF(action = '1', 1, 0)) as uniq_one FROM (SELECT DISTINCT camp, random_id FROM table) t GROUP BY camp
#1054 - Unknown column 'action' in 'field list'
SELECT camp, SUM(IF(action = '0', 1, 0)) as uniq_null, SUM(IF(action = '1', 1, 0)) as uniq_one FROM table GROUP BY camp
SELECT camp, COUNT( DISTINCT random_id), action FROM table group by camp, action
Answer the question
In order to leave comments, you need to log in
SELECT camp, random_id,COUNT(1) FROM table WHERE action = 0 group by camp,randomId
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question