0

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);
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
fsi
  • 1,319
  • 1
  • 23
  • 51

5 Answers5

2
select value, count(*) from a where type=600 group by value
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3741598
  • 297
  • 1
  • 12
2

Is this what you need?

SELECT type, value, COUNT(*)
FROM Answer
GROUP BY 1, 2;
Barto
  • 337
  • 2
  • 11
1

You can group by type and value.

SQL Fiddle Example

 select type,
   value,
   count(*)
 from answer
 -- add your where clause
 group by type, value
SQLChao
  • 7,709
  • 1
  • 17
  • 32
0
select type, count(value) 
from table
where type = 600
group by type

Wouldn't this be all you need?

JanT
  • 2,105
  • 3
  • 28
  • 35
0
SELECT TYPE,VALUE,COUNT(*) FROM Answer 
GROUP BY type,value

You can group based on multiple columns please refer this question for more details.

Community
  • 1
  • 1
M4ver1k
  • 1,505
  • 1
  • 15
  • 26