Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL count columns on specific value

Tags:

mysql

I have the following db table, and I would like to be able to count the instance of sales of certain products per salesperson.

|------------|------------|------------| |id          |user_id     |product_id  | |------------|------------|------------| |1           |1           |2           | |2           |1           |4           | |3           |1           |2           | |4           |2           |1           | |------------|------------|------------| 

I would like to able to create a result set like the following;

|------------|-------------|------------|------------|------------| |user_id     |prod_1_count |prod_2_count|prod_3_count|prod_4_count| |------------|-------------|------------|------------|------------| |1           |0            |2           |0           |1           | |2           |1            |0           |0           |0           | |------------|-------------|------------|------------|------------| 

I am creating graphs with this data, and once again (as earlier today) I am unable to count the column totals. I have tried;

SELECT user_id,  (SELECT count(product_id) FROM sales WHERE product_id = 1) AS prod_1_count, (SELECT count(product_id) FROM sales WHERE product_id = 2) AS prod_2_count, (SELECT count(product_id) FROM sales WHERE product_id = 3) AS prod_3_count, (SELECT count(product_id) FROM sales WHERE product_id = 4) AS prod_4_count  FROM sales GROUP BY user_id;  

I can see why this doesn't work, because for each bracketed SELECT the user_id doesn't match the external user_id in the main SELECT statement.

like image 277
Christy Herron Avatar asked Apr 04 '13 15:04

Christy Herron


People also ask

How do I count the number of particular column values in MySQL?

MySQL COUNT(DISTNCT expression) In MySQL, the COUNT(DISTINCT expression) method is used to sum non-Null values and distinct values of the column 'expression'. To count a distinct number of non-null values in the column 'Age' we have been using the below query.

How count rows with specific value in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

How do you count the number of times a value appears in a column MySQL?

The MySQL COUNT() function allows you to count how many times a certain value appears in your MySQL database. The function can also help you to count how many rows you have in your MySQL table. The function has one expression parameter where you can specify the condition of the query.


2 Answers

You can do this using SUM and CASE:

select user_id,   sum(case when product_id = 1 then 1 else 0 end) as prod_1_count,   sum(case when product_id = 2 then 1 else 0 end) as prod_2_count,   sum(case when product_id = 3 then 1 else 0 end) as prod_3_count,   sum(case when product_id = 4 then 1 else 0 end) as prod_4_count from your_table group by user_id 
like image 199
Ike Walker Avatar answered Sep 20 '22 22:09

Ike Walker


You are trying to pivot the data. MySQL does not have a pivot function so you will have to use an aggregate function with a CASE expression:

select user_id,   count(case when product_id = 1 then product_id end) as prod_1_count,   count(case when product_id = 2 then product_id end) as prod_2_count,   count(case when product_id = 3 then product_id end) as prod_3_count,   count(case when product_id = 4 then product_id end) as prod_4_count from sales group by user_id; 

See SQL Fiddle with Demo

like image 29
Taryn Avatar answered Sep 20 '22 22:09

Taryn