计算当天订阅和退订的总用户

时间:2019-05-30 06:12:05

标签: mysql

我想计算同一天订阅和取消订阅服务的总用户数。 用户为 MSISDN ,订阅为 OperationType 。 如果用户订阅,OperationType = 1,如果用户取消订阅,OperationType =2。用户可以在同一天订阅和取消订阅服务。

订阅:OperationType = 1 退订:OperationType = 2

Structure & Data of tbl_process 参考我的数据。我有两个相同的用户(即MSISDN)进行订阅,后来又取消订阅该服务。

This is my result based on query below. I somehow manage to get list of user that subscribe and unsubscribe on the same day. But now I want to count total user who got 2 OperationType, which is 1 & 2 (this is based on my query).

以下是我的查询:

SELECT DISTINCT(msisdn), operationType
FROM tbl_process
WHERE DATE(RequestTS) = '2019-03-14'
AND STATUS = 3
ORDER BY msisdn;

3 个答案:

答案 0 :(得分:0)

从中选择DATE,count(*) (选择  date(RequestTs)为DATE,  msisdn,   计数(不同的操作类型) 从   tbl_process t1 分组1  具有count(distinct operationType)> 1  )被1分组

从中选择计数(distinct msisdn) (选择  date(RequestTs)为DATE,  msisdn,   计数(不同的操作类型) 从   tbl_process t1 分组1  具有count(distinct operationType)> 1  )被1分组

答案 1 :(得分:0)

最终答案。我得到答案了

SELECT SUM(IF(x.sub > 0 AND x.unsub > 0, 1, 0)) AS Daychrn
FROM ( 
	SELECT DISTINCT(msisdn), 
	SUM(IF(operationtype='1', 1, 0)) AS sub, 
	SUM(IF(operationtype='2', 1, 0)) AS unsub 
	FROM tbl_process 
	WHERE processts LIKE '2019-04-14%' 
	AND STATUS =3 
	GROUP BY msisdn
) X;

答案 2 :(得分:-1)

尝试一下

select 
  count(*) 
from 
  tbl_process t1 
  inner join tbl_process t2 on DATE_FORMAT(t1.RequestTS, '%m-%d') = DATE_FORMAT(t2.RequestTS, '%m-%d') 
  and t1.msisdn = t2.msisdn 
  and t1.operationType = 1 
  and t2.operationType = 2;