Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of grouped rows in mysql

Tags:

sql

mysql

count

In a table xyz I have a row called components and a labref row which has labref number as shown here

Table xyz

labref             component NDQA201303001          a NDQA201303001          a NDQA201303001          a NDQA201303001          a NDQA201303001          b NDQA201303001          b NDQA201303001          b NDQA201303001          b NDQA201303001          c NDQA201303001          c NDQA201303001          c NDQA201303001          c 

I want to group the components then count the rows returned which equals to 3, I have written the below SQL query but it does not help achieve my goal instead it returns 4 for each component

SELECT DISTINCT component, COUNT( component )  FROM `xyz` WHERE labref = 'NDQA201303001' GROUP BY component 

The query returns

Table xyz

labref         component   COUNT(component)        NDQA201303001   a           4 NDQA201303001   b           4 NDQA201303001   c           4 

What I want to achieve now is that from the above result, the rows are counted and 3 is returned as the number of rows, Any workaround is appreciated

like image 676
alphy Avatar asked May 16 '13 10:05

alphy


People also ask

How do I count rows in MySQL by group?

So, if you want to count quantity of groups, not quantity of elements in each group, and return duplicate value to every group record in result table, you should use OVER() clause on you'r count function.

How do I count the number of rows per group in SQL?

To count the number of rows, use the id column which stores unique values (in our example we use COUNT(id) ). Next, use the GROUP BY clause to group records according to columns (the GROUP BY category above). After using GROUP BY to filter records with aggregate functions like COUNT, use the HAVING clause.

How do I count rows in MySQL?

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.

How do I count by GROUP BY?

The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.


2 Answers

Try this simple query without a sub-query:

SELECT COUNT(DISTINCT component) AS TotalRows FROM xyz WHERE labref = 'NDQA201303001'; 

See this SQLFiddle

like image 190
Himanshu Jansari Avatar answered Sep 25 '22 09:09

Himanshu Jansari


You need to do -

SELECT     COUNT(*) FROM     (         SELECT             DISTINCT component         FROM             `multiple_sample_assay_abc`         WHERE             labref = 'NDQA201303001'     ) AS DerivedTableAlias 

You can also avoid subquery as suggested by @hims056 here

like image 33
Kshitij Avatar answered Sep 23 '22 09:09

Kshitij