如何计算超过 1 个有子句 mysql [英] how to count with more than 1 having clause mysql

查看:56
本文介绍了如何计算超过 1 个有子句 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

然后我每月汇总数据

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

在这种情况下,我想找出 3 月份每个明星成员(users_id 在一个月内交易 >= 600000)的分布,以及 users_Id 在 3 月之前进行交易的位置 >= 600.000(如果 users_Id 在第一次三月,则users_Id进入三月统计)

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

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

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

这是我目前的进度语法

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

这是小提琴https://dbfiddle.uk/?rdbms=mysql_5.7&42900f967d7af9659d8639d7/a>

here's the 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

您的 MySQL 服务器版本是什么?– 秋奈

What is your MySQL server version? – Akina

这是 5.7.23-log – Fachry Dzaky Al-Qadri Sabil

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

小提琴

明星会员三月统计的预期结果

expected results for star member on month march statistics

...

嗯... users_id=13 没有被计算在内,因为他没有在三月出演???– 秋奈

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

是的,因为我想在 3 月做统计,而 users_id 13 没有被计算在内,因为他们不是 3 月的明星会员(交易 >= 600000)– Fachry Dzaky Al-Qadri Sabil

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

小提琴

这篇关于如何计算超过 1 个有子句 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆