Search code examples
mysqlcountmultiple-columnscase-when

MySQL which query statement should be used?


I have a dataset like this as shown below, for example row 1, 0 purchase is made for the item priced at $3, 3 purchases are made for item priced at $30. I would need to write a query to provide the summary of how many passes purchased, by categorising the prices into price range of "0-10", "11-20", "21-30", "31-40". I assume case when should be used but I am unsure of how. Please help.

+-----------------+-----------------+--------------------------+--------------------------+
| price_category1 | price_category2 | purchase_count_category1 | purchase_count_category2 |
+-----------------+-----------------+--------------------------+--------------------------+
|               3 |              30 |                        0 |                        3 |
|              20 |               6 |                        1 |                        4 |
|              25 |              11 |                        4 |                        0 |
|              17 |              12 |                        0 |                        1 |
+-----------------+-----------------+--------------------------+--------------------------+

+------+-------+-------+-------+
| 0-10 | 11-20 | 21-30 | 31-40 |
+------+-------+-------+-------+
|    4 |     2 |     7 |     0 |
+------+-------+-------+-------+

Solution

  • SELECT SUM(CASE WHEN price_category1 BETWEEN 0 AND 10 
                    THEN purchase_count_category1 
                    END) + SUM(CASE WHEN price_category2 BETWEEN 0 AND 10  
                                    THEN purchase_count_category2 
                                    END) AS `0-10`,
           SUM(CASE WHEN price_category1 BETWEEN 11 AND 20
                    THEN purchase_count_category1 
                    END) + SUM(CASE WHEN price_category2 BETWEEN 11 AND 20
                                    THEN purchase_count_category2 
                                    END) AS `11-20`,
           SUM(CASE WHEN price_category1 BETWEEN 21 AND 30 
                    THEN purchase_count_category1 
                    END) + SUM(CASE WHEN price_category2 BETWEEN 21 AND 30
                                    THEN purchase_count_category2 
                                    END) AS `21-30`
    FROM source_table
    

    or

    SELECT SUM(CASE WHEN cat BETWEEN 0 AND 10 
                    THEN cnt
                    END )  AS `0-10`,
           SUM(CASE WHEN cat BETWEEN 11 AND 20
                    THEN cnt
                    END )  AS `11-20`,
           SUM(CASE WHEN cat BETWEEN 21 AND 30 
                    THEN cnt
                    END )  AS `21-30`
    FROM ( SELECT price_category1 cat, purchase_count_category1 cnt
           FROM source_table
           UNION ALL
           SELECT price_category2, purchase_count_category2
           FROM source_table ) src
    

    PS. This solution gives "horizontal" output - one row with all needed statistic. If you need "vertical" output then use the solution provided by ismetguzelgun.

    Pay attention - my solution is not extendable (if you need to alter the ranges amount or borders you must alter the query text) whereas alternative solution can be extended easily after converting hardcoded ranges borders to according CTE or (the best) additional criteria table.