SQL Server - Query to Count

Asked By Ajay
11-Feb-12 09:02 AM
Hi All
Is there any possibility of counting the occourence of two type of values
For Eg: yes/no from same Coloumn of a single table with a single Query  

regards 
ajay
  Sreekumar P replied to Ajay
11-Feb-12 09:44 AM
Hi,

If u mean the count(occurrence) as Rows


SELECT COUNT(value) FROM table GROUP BY value

First row will return the first occurrence count, and 2nd the other



If u mean the count(occurrence) as Columns

SELECT COUNT(value) as YesCount ,(SELECT COUNT(value) as YesCount
FROM table  WHERE value='no') as NoCount
FROM table  WHERE value='yes'




 
  D Company replied to Ajay
11-Feb-12 11:00 AM
Ajay,

Yes, it is possible, simply u can write your query like this.

if there are only two values in the table than u can count,

SELECT COUNT(DISTINCT column_name) FROM table_name

Regards
D
  kalpana aparnathi replied to Ajay
11-Feb-12 12:08 PM
hi,

Try this way:

SELECT
  COUNT(CASE c_type WHEN 'y' THEN 1 END) AS Active,
  COUNT(CASE c_type WHEN 'n' THEN 1 END) AS Not_Active,
FROM tblname
WHERE c_type IN ('y', 'n')
  Sandeep Mittal replied to D Company
12-Feb-12 01:14 AM
Dear D Company

"SELECT COUNT(DISTINCT column_name) FROM table_name".
This would give the count of distinct values in the column, but i believe the requirement of ajay is to find the count of occurence of distinct values.

Check this for your reference

DECLARE @tab TABLE(val VARCHAR(10))
INSERT INTO @tab
SELECT 'yes' UNION ALL SELECT 'yes' UNION ALL SELECT 'yes' UNION ALL SELECT 'no'
SELECT COUNT(DISTINCT val) FROM @tab


Ajay use "group by" to find the the count of occurence of all distinct values of a column
Example for your reference

DECLARE @tab TABLE(val VARCHAR(10))
INSERT INTO @tab
SELECT 'yes' UNION ALL SELECT 'yes' UNION ALL SELECT 'yes' UNION ALL SELECT 'no'
SELECT val, COUNT(1) FROM @tab GROUP BY val
  Somesh Yadav replied to Ajay
12-Feb-12 03:47 AM

Hi here is an example .
The COUNT function returns the number of rows in a query.

The syntax for the COUNT function is:

SELECT COUNT(expression)
FROM tables
WHERE predicates;


Note:

The COUNT function will only count those records in which the field in the brackets is NOT NULL.

For example, if you have the following table called suppliers:

Supplier_ID Supplier_Name State
1 IBM CA
2 Microsoft  
3 NVIDIA  

The result for this query will return 3.

Select COUNT(Supplier_ID) from suppliers;

While the result for the next query will only return 1, since there is only one row in the suppliers table where the State field is NOT NULL.

Select COUNT(State) from suppliers;


Simple Example

For example, you might wish to know how many employees have a salary that is above $25,000 / year.

SELECT COUNT(*) as "Number of employees"
FROM employees
WHERE salary > 25000;

In this example, we've aliased the count(*) field as "Number of employees". As a result, "Number of employees" will display as the field name when the result set is returned.


Example using DISTINCT

You can use the DISTINCT clause within the COUNT function.

For example, the SQL statement below returns the number of unique departments where at least one employee makes over $25,000 / year.

SELECT COUNT(DISTINCT department) as "Unique departments"
FROM employees
WHERE salary > 25000;

Again, the count(DISTINCT department) field is aliased as "Unique departments". This is the field name that will display in the result set.


Example using GROUP BY

In some cases, you will be required to use a GROUP BY clause with the COUNT function.

For example, you could use the COUNT function to return the name of the department and the number of employees (in the associated department) that make over $25,000 / year.

SELECT department, COUNT(*) as "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the COUNT function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.

  R B replied to Ajay
13-Feb-12 07:16 AM

Hello,

 

You need to use case. Using Case first you have ‘Yes’ record to set 1 and then sum of this value. Same thing doing in ‘No’ record.

Try your Query as following way

 

select

    SUM(Case when FieldName = 'Yes' then 1 else 0 end) YesCount,

    SUM(Case when FieldName = 'No' then 1 else 0 end) NoCount

from TableName

 

 

Hope this is helpful !

Thanks

 

 

 

 

 

Create New Account
help
I am a newbie and I would like to know the difference between the two: select count(*) from tableA select count(1) from tableA select count(10) from tableA Thanks. SQL Server Programming Discussions SQL Server (1) CREATE TABLE (1) There no difference. The optimizer is smart enough to know that you do not need to count "all columns" to count the number of rows. No difference at all. COUNT(<expression> ) just means "count
query optymilization SQL Server Hello, i have table with columns plec, stopien, lokalizacja, oddzial and i need sum count use this query : SELECT COUNT(*) AS ilosc_osob, (SELECT COUNT(*) AS Expr1 FROM tabela WHERE (lokalizacja LIKE 'warszawa') AND (wydzial LIKE 'administracja') AND (plec LIKE kobieta')) AS kobiety, (SELECT COUNT(*) AS Expr1 FROM tabela AS tabela_2 WHERE (lokalizacja LIKE 'warszawa') AND (wydzial LIKE 'administracja') AND
Return Count results from several queries as single row SQL Server I have a few COUNT queries that i run across several tables eg select count(ID) from table1 where . . . . select count(ID) from table2 where . . . . select count(ID) from table3 where . . . . works fine, and the results are returned as 3 4 5
select statement in variables declare @leftuser int declare @rightuser int set @leftuser = (select COUNT(username) from Distributors where TreePosition = 'l' group by SponsorID) set @rightuser = (select COUNT(username) from Distributors where TreePosition = 'r' group by SponsorID) select @leftuser, @rightuser , UserName from Distributors hiii my this query is not working well kindly help follows = , ! = , <, < = , > , > = or when the subquery is used as an expression. hi, Try with this way: select @leftuser = COUNT (username) from Distributors where TreePosition = 'l' group by SponsorID) select @rightuser = COUNT (username) from Distributors