Reputation: 1367
I have my table below:
| id | version | type | test_id | value |
+------+---------+----------+---------+--------------------+
| 1235 | 0 | 600 | 111 | 2 |
| 1236 | 0 | 600 | 111 | 2 |
| 1237 | 0 | 600 | 111 | 2 |
| 1238 | 0 | 601 | 111 | 1 |
| 1239 | 0 | 602 | 111 | 1 |
| 1240 | 0 | 600 | 111 | 1 |
| 1241 | 0 | 601 | 111 | 1 |
I'm trying to retrieve the count dependents of the column value. Type 600 has three values of 2 and one value of 1. So I need the result 3 and 1. My co-worker told me to use distinct but I think I'm using wrong syntax?
(select distinct a.type from Answer a where type = 600)
union
(select distinct a.value from Answer a where type = 600)
union
(select count(value) from Answer where type = 600 and value = 2);
Upvotes: 0
Views: 151
Reputation: 2096
select type, count(value)
from table
where type = 600
group by type
Wouldn't this be all you need?
Upvotes: 0
Reputation: 1575
SELECT TYPE,VALUE,COUNT(*) FROM Answer
GROUP BY type,value
You can group based on multiple columns please refer this question for more details.
Upvotes: 0
Reputation: 7837
You can group by type and value.
select type,
value,
count(*)
from answer
-- add your where clause
group by type, value
Upvotes: 1
Reputation: 377
Is this what you need?
SELECT type, value, COUNT(*)
FROM Answer
GROUP BY 1, 2;
Upvotes: 2