如何计算超过 1 个 having 子句 mysql

how to count with more than 1 having clause mysql

我有1个table叫order_star_member,其中createdAt为交易日期,users_id为买家,total_price_star_member为交易金额.在这种情况下,我想找出拥有明星会员的买家(一个月内的交易 >= 600000)并找出他们来自哪里,order_star_member 的数据(虚拟)从 2019 年 1 月开始到 2019 年 3 月

    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;

然后我每个月汇总数据

-- 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;

在这种情况下,我想找出每个明星会员(users_id 一个月内交易 >= 600000)的分布,以及 users_Id 在何处进行他的交易 >= 600.000 3月前(如果users_Id3月第一次做交易,则users_Id进入3月至3月统计)

expected results for star member on month march statistics

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

解释:有 2 个 users_Id 在 3 月 (users_Id 14,15) 成为 star_member,并在 2019 年 1 月成为他的第一个 star_member, 有 1 users_id 在 3 月成为 star_member(users_id 12)并在 2019 年 2 月成为他的第一个 star_member, 有 3 个 users_id 在 3 月(users_id 16、17、19)成为 star_member,并在 2019 年 3 月 star_member

成为他的第一个 star_member

这是我到目前为止的进度语法

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

这是 fiddle https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=d642774357900f967d7af9659d8639d7

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

据我了解,统计中统计的用户必须在本月加星标。如果是

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