Search code examples
mysqlmysql-workbenchmysql-error-1064

how to count with more than 1 having clause mysql


i have 1 table called order_star_member, which contain createdAt as the date of the transaction, users_id as the buyer, total_price_star_member as the amount of the transaction. on this case i want to find out buyer who had star member (transaction in a month within >= 600000) and find out where they coming from, the data(dummy) for order_star_member begin on January 2019 untill March 2019

    CREATE TABLE order_star_member  ( users_id INT, 
                                       createdAt DATE, 
                                       total_price_star_member DECIMAL(10,2) );
INSERT INTO order_star_member  VALUES
(12,'2019-01-01',100000),
(12,'2019-01-10',100000),
(12,'2019-01-20',100000),
(12,'2019-02-10',100000),
(12,'2019-02-15',300000),
(12,'2019-02-21',500000),
(13,'2019-01-02',900000),
(13,'2019-01-11',300000),
(13,'2019-01-18',400000),
(13,'2019-02-06',100000),
(13,'2019-02-08',900000),
(13,'2019-02-14',400000),
(14,'2019-01-21',500000),
(14,'2019-01-23',200000),
(14,'2019-01-24',300000),
(14,'2019-02-08',100000),
(14,'2019-02-09',200000),
(14,'2019-02-14',100000),
(15, '2019-03-04',1000000),
(14, '2019-03-04', 300000),
(14, '2019-03-04', 350000),
(13, '2019-03-04', 400000),
(15, '2019-01-23', 620000),
(15, '2019-02-01', 650000),
(12, '2019-03-03', 750000),
(16, '2019-03-04', 650000),
(17, '2019-03-03', 670000),
(18, '2019-02-02', 450000),
(19, '2019-03-03', 750000);
SELECT * from order_star_member;

and then i summarize data per month

-- summary per-month data
SELECT users_id, 
       SUM( CASE WHEN MONTHNAME(createdAt) = 'January' 
                 THEN total_price_star_member
                 END ) total_price_star_member_January,
       SUM( CASE WHEN MONTHNAME(createdAt) = 'February' 
                 THEN total_price_star_member
                 END ) total_price_star_member_February,
        SUM( CASE WHEN MONTHNAME(createdAt) = 'March' 
                 THEN total_price_star_member
                 END ) total_price_star_member_March
FROM order_star_member 
GROUP BY users_id
ORDER BY 1;

on this case i want to find out the distribution for each star member (users_id who transaction >= 600000 in a month) in march and where the users_Id doing his transaction >= 600.000 before march (if the users_Id doing transaction on the first time in march, then the users_Id enter the march to march statistic)

expected results for star member on month march statistics

    +-------------------+------+-------+
    | count star_member | year | month |  
    +-------------------+------+-------+
    |                 2 | 2019 |     1 |  
    |                 1 | 2019 |     2 |  
    |                 3 | 2019 |     3 |  
    +-------------------+------+-------+

explanation : there's 2 users_Id who become star_member on march (users_Id 14,15) and become his first star_member on january 2019, there's 1 users_id who become star_member on march (users_id 12) and become his first star_member on february 2019, and there's 3 users_id who become star_member on march (users_id 16, 17, 19) and become his first star_member on march 2019

this is my progress syntax so far

select count(osm.users_id) as count_buyer, year(osm.createdAt) as year,
month(osm.createdAt) as month
 from order_star_member osm
WHERE osm.createdAt >= MAKEDATE(YEAR(osm.createdAt), 1) 
+ INTERVAL month(osm.createdAt) month- INTERVAL 1 month AND
 osm.createdAt <= MAKEDATE(YEAR(osm.createdAt), 1)
 + INTERVAL month(osm.createdAt) month- INTERVAL 1 day group by users_Id
 having sum(total_price_star_member) >= 600000
 AND 
 NOT EXISTS (SELECT 1 from order_star_member osm2
 WHERE osm2.users_id = osm.users_id 
 AND osm2.createdAt < MAKEDATE(YEAR(osm.createdAt), 1) + 
 INTERVAL month(osm.createdAt) month - INTERVAL 1 month
 group by users_id 
 having sum(total_price_star_member) >= 600000)
 AND 
 EXISTS (SELECT 1 from order_star_member osm3
 where
 osm3.users_id = osm.users_id AND
 osm3.createdAt >= '2019-03-01' AND
 osm3.createdAt < '2019-04-01' group by users_id
 having sum(total_price_star_member) >= 600000)
 group by year(osm.createdAt), month(osm.createdAt);

here's the fiddle https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=d642774357900f967d7af9659d8639d7


Solution

  • WITH 
    cte1 AS ( SELECT DISTINCT
                     users_id, 
                     DATE_FORMAT(createdAt, '%Y%m') year_and_month,
                     SUM(total_price_star_member) OVER (PARTITION BY users_id, DATE_FORMAT(createdAt, '%Y%m')) month_price
              FROM order_star_member ),
    cte2 AS ( SELECT users_id, MIN(year_and_month) year_and_month
              FROM cte1
              WHERE month_price >= 600000
              GROUP BY users_id )
    SELECT COUNT(users_id) count_star_member,
           year_and_month DIV 100 `year`,
           year_and_month MOD 100 `month`
    FROM cte2
    GROUP BY year_and_month
    ORDER BY `year`, `month`;
    

    fiddle


    What is your MySQL server version? – Akina

    it's 5.7.23-log – Fachry Dzaky Al-Qadri Sabil

    SELECT COUNT(users_id) count_star_member,
           year_and_month DIV 100 `year`,
           year_and_month MOD 100 `month`
    FROM (SELECT users_id, 
                 MIN(year_and_month) year_and_month
          FROM ( SELECT users_id, 
                        DATE_FORMAT(createdAt, '%Y%m') year_and_month,
                        SUM(total_price_star_member) month_price
                 FROM order_star_member
                 GROUP BY users_id, 
                          DATE_FORMAT(createdAt, '%Y%m') 
                 HAVING month_price >= 600000 ) starrings
          GROUP BY users_id ) first_starrings
    GROUP BY year_and_month
    ORDER BY `year`, `month`;
    

    fiddle


    expected results for star member on month march statistics

    ...

    Hmm... users_id=13 is NOT counted because he is NOT starred in March??? – Akina

    yes, because i want to make statistic on month march, and users_id 13 not counted because they not being star member on march (transaction >= 600000) – Fachry Dzaky Al-Qadri Sabil

    As I understand the user counted in this statistic MUST be starred in this month. If so

    SELECT COUNT(users_id) count_star_member,
           year_and_month DIV 100 `year`,
           year_and_month MOD 100 `month`
    FROM (SELECT users_id, 
                 MIN(year_and_month) year_and_month
          FROM ( SELECT users_id, 
                        DATE_FORMAT(createdAt, '%Y%m') year_and_month,
                        SUM(total_price_star_member) month_price
                 FROM order_star_member
                 GROUP BY users_id, 
                          DATE_FORMAT(createdAt, '%Y%m') 
                 HAVING month_price >= 600000 ) starrings
          GROUP BY users_id
          HAVING SUM(year_and_month = '201903') > 0 ) first_starrings
    GROUP BY year_and_month
    ORDER BY `year`, `month`;
    

    fiddle