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.
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.
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).
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.
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
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
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