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);

5 Answers 5

2

Is this what you need?

SELECT type, value, COUNT(*)
FROM Answer
GROUP BY 1, 2;
Sign up to request clarification or add additional context in comments.

1 Comment

This answer could be more generic by not hard coding for value 1 or 2.
2
select value, count(*) from a where type=600 group by value

Comments

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

Comments

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.

2 Comments

From each line, it gives me 1. Which the case I'm trying is get 3 and 1 value.
please include other necessary fields as well , i have edited the answer.
0
select type, count(value) 
from table
where type = 600
group by type

Wouldn't this be all you need?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.