【问题标题】:MySQL find transactions (payments) of customers that have not purchased for the first timeMySQL 查找未首次购买的客户的交易(付款)
【发布时间】:2015-07-29 19:03:13
【问题描述】:

我有一个包含交易的简单表格,我想了解每个月有多少消费者进行了总计超过 0 的交易,并且他们的第一笔交易不在该月。首次交易是指客户在当月第一次购买。

我想要得到的结果如下:

+--------+---------+-----------------------------------+
|  Year  |  Month  |  NumOfCustomersWithPositiveTotals |
+--------+----------------------------+----------------+
|  2014  |    1    |                 22                |
+--------+----------------------------+----------------+
|  2014  |    2    |                 10                |
+--------+----------------------------+----------------+

我有一个 SQL 小提琴,我可以在其中找到相同的东西,但适用于当月内完成第一笔交易的消费者。实际上,我正在寻找的查询是相同的,但对于其他消费者而言。

这是小提琴:http://sqlfiddle.com/#!9/31538/24

【问题讨论】:

    标签: mysql payment


    【解决方案1】:

    我想这就是你想要的:

    SELECT count(consumerId) as NumOfCust, mm, yy FROM
    (
        SELECT consumerId, month(date) as mm, year(date) as yy, sum(amount) as total, mdate FROM beta.transaction as t
        LEFT JOIN (
                SELECT min(month(date)) as mdate, consumerId as con FROM beta.transaction
                GROUP BY consumerId
                ) as MinDate ON con = t.consumerId
        GROUP BY month(date), consumerId
        HAVING mdate < mm AND total > 0
    ) as res
    GROUP BY res.mm;
    

    我会试着从里到外解释它

    让我们看看名为 minDate 的 JOIN 表有什么:

    SELECT min(month(date)) as mdate, consumerId as con FROM beta.transaction
    GROUP BY consumerId
    
    -- Here we find the first date of transaction per consumerId
    

    下一个

    SELECT consumerId, month(date) as mm, year(date) as yy, sum(amount), mdate FROM beta.transaction as t
        LEFT JOIN (
                SELECT min(month(date)) as mdate, consumerId as con FROM beta.transaction
                GROUP BY consumerId
                ) as MinDate ON con = t.consumerId
        GROUP BY month(date), consumerId
        HAVING mdate < mm AND total > 0
    
     -- Here we find total amount per consumerId per month and count only the consumers whose first transact (aka minDate) is lower than current month AND total is greater than 0
    

    最后我们用外部 SELECT 来统计上面的按月分组的结果。 我希望它是你想要的。

    【讨论】:

    • 非常感谢您的回复!我稍微更改了您的查询,并在下面发布了一个符合我需要的答案。
    【解决方案2】:

    下面的查询返回正确的结果。答案基于 Akis 的答案,即在 group by 子句中添加 consumerId 并在支票中添加年份和月份。

    select
        tyyyy, tmm, count(tcon) as oldkund_real
    from
    (
        select
            t.yyyy as tyyyy, t.mm as tmm, t.consumerId as tcon, sum(t.amount) as total, fp.yyyy as fpyyyy, fp.mm as fpmm, fp.consumerId as fpcon
        from
            (
                select
                    year(date) as yyyy, month(date) as mm, consumerId, amount
                from
                    transaction
            ) as t
            left join
            (
                select
                    year(min(date)) as yyyy, month(min(date)) as mm, consumerId
                from
                    transaction
                group by
                    consumerid
            ) as fp
            on
                t.consumerId = fp.consumerId
            group by
                t.yyyy, t.mm, t.consumerId
            having
                STR_TO_DATE(CONCAT('01,', tmm, ',', tyyyy),'%d,%m,%Y') > STR_TO_DATE(CONCAT('01,', fpmm, ',', fpyyyy),'%d,%m,%Y') and total > 0
    ) as res
    group by
        tyyyy, tmm
    order by
        tyyyy, tmm;
    

    非常感谢阿基斯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 2018-04-21
      • 2021-06-04
      • 1970-01-01
      相关资源
      最近更新 更多