Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select count(*) from result query

I need help from you, this is my sql query:

select count(SID) 
from Test 
where Date = '2012-12-10' 
group by SID

this is my result:

|2|
|3|
|4|
|3|

and now I have to count the results from first query!

Expected result: 4 
like image 525
Butters Avatar asked Dec 12 '12 10:12

Butters


People also ask

What is the result of given query SELECT COUNT (*) from table?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.

What is SELECT COUNT (*) in SQL?

SQL SELECT COUNT(*) function The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

How do I write a SELECT COUNT in SQL query?

Syntax of Select Count Function in SQL In the syntax, we have to specify the column's name after the COUNT keyword and the name of the table on which the Count function is to be executed.

How do you COUNT rows of query results?

Counting all of the Rows in a Table. To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.


3 Answers

You can wrap your query in another SELECT:

select count(*)
from
(
  select count(SID) tot  -- add alias
  from Test 
  where Date = '2012-12-10' 
  group by SID
) src;  -- add alias

See SQL Fiddle with Demo

In order for it to work, the count(SID) need a column alias and you have to provide an alias to the subquery itself.

like image 125
Taryn Avatar answered Oct 04 '22 10:10

Taryn


This counts the rows of the inner query:

select count(*) from (
    select count(SID) 
    from Test 
    where Date = '2012-12-10' 
    group by SID
) t

However, in this case the effect of that is the same as this:

select count(distinct SID) from Test where Date = '2012-12-10'
like image 24
dan1111 Avatar answered Oct 04 '22 11:10

dan1111


select count(*) from(select count(SID) from Test where Date = '2012-12-10' group by SID)select count(*) from(select count(SID) from Test where Date = '2012-12-10' group by SID)

should works

like image 23
Daniel Mács Avatar answered Oct 04 '22 10:10

Daniel Mács