Reputation: 2706
I have a table tbl_demo
like this:
aliasname is_correct
10 INCORRECT
10 INCORRECT
10 INCORRECT
10 CORRECT
10 CORRECT
10 NOT SOLVED
9 INCORRECT
9 INCORRECT
9 CORRECT
I created a query:
select
aliasname,
count(*) as quest_count,
is_correct
from
tbl_demo
group by
aliasname, is_correct order by aliasname
and I get output like this
aliasname quest_count is_correct
---------------------------------
10 2 CORRECT
10 3 INCORRECT
10 1 NOT SOLVED
9 1 CORRECT
9 2 INCORRECT
but I want 0 for in 9 and NOT solved
Like one more row like this
9 0 NOT SOLVED
Can this be achieved easily, and how?
Upvotes: 1
Views: 62
Reputation: 1269773
You can do this by generating all possible combinations using cross join
, and the using left join
to bring in the original data. The rest is just aggregation:
select a.aliasname, count(d.aliasname) as quest_count, ic.is_correct
from (select distinct aliasname from tbl_demo
) a cross join
(select distinct is_correct from tbl_demo
) ic left outer join
tbl_demo d
on d.aliasname = a.aliasname and d.is_correct = ic.is_correct
group by a.aliasname, ic.is_correct;
Upvotes: 3