Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counts on single column for different conditions

I require 3 different counts on single column for different conditions.

Table structure:

interview-
id-int(10)
c_id-int(10)
experience-varchar2(100)

experience have 3 different values- 1)positive 2)negative 3)neutral

I require 3 different counts of "count_positive", "count_negative" and "count_neutral" for where condition of c_id=10.

I know it can get by 3 different queries. Can I able to get 3 counts by single query?

like image 912
Somnath Muluk Avatar asked Nov 30 '11 05:11

Somnath Muluk


People also ask

How can we count the number of some attribute based on some given conditions?

Overall, you can use * or ALL or DISTINCT or some expression along with COUNT to COUNT the number of rows w.r.t. some condition or all of the rows, depending up on the arguments you are using along with COUNT() function. Applies to all values. ALL returns the number of non NULL values.

How do I get a count of a specific column?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How do I count multiple values in one column in SQL?

You can count multiple COUNT() for multiple conditions in a single query using GROUP BY. SELECT yourColumnName,COUNT(*) from yourTableName group by yourColumnName; To understand the above syntax, let us first create a table. The query to create a table is as follows.

How do I count specific items in a column in Excel?

Use the COUNT function to get the number of entries in a number field that is in a range or array of numbers. For example, you can enter the following formula to count the numbers in the range A1:A20: =COUNT(A1:A20). In this example, if five of the cells in the range contain numbers, the result is 5.

Can you use count with multiple columns?

Obviously, COUNT(DISTINCT) with multiple columns counts unique combinations of the specified columns' values. However, one other important point is that a tuple is counted only if none of the individual values in the tuple is null.

How do I count multiple values in one cell?

You can use the COUNTIFS function in Excel to count cells in a single range with a single condition as well as in multiple ranges with multiple conditions. If the latter, only those cells that meet all of the specified conditions are counted.

How do you give a condition to a count function?

COUNT() with HAVINGThe HAVING clause with SQL COUNT() function can be used to set a condition with the select statement. The HAVING clause is used instead of WHERE clause with SQL COUNT() function.

How do I count the number of times a value appears in a column in Excel?

Count how often a single value occurs by using the COUNTIF function. Use the COUNTIF function to count how many times a particular value appears in a range of cells.


2 Answers

SELECT
   SUM(CASE experience
          WHEN 'positive' THEN 1
          ELSE 0
       END) AS CountPositive
   , SUM(CASE experience
            WHEN 'negative' THEN 1
            ELSE 0
         END) AS CountNegative
   , SUM(CASE experience
            WHEN 'neutral' THEN 1
            ELSE 0
         END) AS CountNeutral
FROM Interview
WHERE c_id = 10
like image 75
Adam Wenger Avatar answered Sep 19 '22 11:09

Adam Wenger


select 'Positive Count' , count(*)
from interview
where experience = 'positive'
UNION
select 'Negative Count' , count(*)
from interview
where experience = 'negative'
UNION
select 'Neutral' , count(*)
from interview
where experience = 'neutral'
like image 42
Zohaib Avatar answered Sep 18 '22 11:09

Zohaib