MySQL查找第一次未购买的客户的交易(付款)
我有一个简单的表,里面有交易,我想得到,每个月,有多少消费者的交易总数超过0,而他们的第一笔交易不是在那个月。我们所说的第一笔交易是指客户在该月内首次购买 我试图得到的结果如下:MySQL查找第一次未购买的客户的交易(付款),mysql,payment,Mysql,Payment,我有一个简单的表,里面有交易,我想得到,每个月,有多少消费者的交易总数超过0,而他们的第一笔交易不是在那个月。我们所说的第一笔交易是指客户在该月内首次购买 我试图得到的结果如下: +--------+---------+-----------------------------------+ | Year | Month | NumOfCustomersWithPositiveTotals | +--------+----------------------------+-------
+--------+---------+-----------------------------------+
| Year | Month | NumOfCustomersWithPositiveTotals |
+--------+----------------------------+----------------+
| 2014 | 1 | 22 |
+--------+----------------------------+----------------+
| 2014 | 2 | 10 |
+--------+----------------------------+----------------+
我有一个SQL小提琴,我发现同样的事情,但消费者有他们的第一笔交易在该月内。实际上,我正在寻找的查询是相同的,但对于其他消费者来说是一样的
这是小提琴:我想这就是你想要的:
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;
下一个
最后,通过外部选择,我们按月对上述结果进行分组统计。
我希望这是您想要的。下面的查询返回正确的结果。答案基于Akis的答案,在GROUPBY子句中添加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;
非常感谢阿基斯 非常感谢您的回复!我对你的问题做了一点修改,我在下面贴了一个答案,这正是我所需要的。
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
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;