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
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.
SQL SELECT COUNT(*) function The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).
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.
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.
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.
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'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With