放弃的注册逻辑#

时间:2019-04-01 19:57:01

标签: sql-server

我正在尝试使用MS SQL查找注册数据的放弃#指标。开始注册的用户数量,在1个日历天内未完成注册。 状态详细信息='NextButton'指示开始注册。另外,我想排除开始注册但其帐户已经存在的用户,该用户由 statusDetail ='ExistingAccount'指示。

逻辑:在1个日历日内未成功完成注册的注册尝试。

该代码从基本过滤器开始这样

            SELECT *
            FROM   Report.rpt.Logs
            WHERE EventName = 'Login' AND EventType='Enrollment'

实际数据:

+-------------------------+----------------+-----------+------------+----------------------+
|     TransactionDate     |  ServiceLogId  | EventName | EventType  |     StatusDetail     |
+-------------------------+----------------+-----------+------------+----------------------+
| 2000-01-01 00:00:00.000 | ss@gmail.com   | LOGIN     | Enrollment | NextButton           |
| 2015-05-12 06:53:02.957 | cvbb@gmail.com | LOGIN     | Enrollment | NextButton           |
| 2015-05-12 06:53:38.867 | cvbb@gmail.com | LOGIN     | Enrollment | ExistingAccount      |
| 2015-03-01 06:53:45.187 | abc@gmail.com  | LOGIN     | Enrollment | NextButton           |
| 2015-03-05 06:53:45.187 | vrc@gmail.com  | LOGIN     | Enrollment | NextButton           |
| 2015-04-23 06:53:45.187 | sr@gmail.com   | LOGIN     | Enrollment | NextButton           |
| 2015-04-23 06:55:47.187 | sr@gmail.com   | LOGIN     | Enrollment | Successfully Created |
+-------------------------+----------------+-----------+------------+----------------------+

预期结果

+--------------------------+----------------+-----------+------------+--------------+
|      TransactionDate     |  ServiceLogId  | EventName | EventType  | StatusDetail |
+--------------------------+----------------+-----------+------------+--------------+
|  2000-01-01 00:00:00.000 | ss@gmail.com   | LOGIN     | Enrollment | NextButton   |
|  2015-03-01 06:53:45.187 | abc@gmail.com  | LOGIN     | Enrollment | NextButton   |
|  2015-03-05 06:53:45.187 | vrc@gmail.com  | LOGIN     | Enrollment | NextButton   |
+--------------------------+----------------+-----------+------------+--------------+

2 个答案:

答案 0 :(得分:1)

这是WHERE NOT EXISTS子句的良好应用。下面的两个NOT EXISTS会过滤掉您的条件之一。

SELECT
  *
FROM
  @logs AS l
WHERE
  l.EventName = 'LOGIN'
  AND l.EventType = 'Enrollment'
  AND l.StatusDetail = 'NextButton'
  AND NOT EXISTS
(--Filter off Existing Accounts
  SELECT
    1
  FROM
    @logs AS s
  WHERE
    s.ServiceLogId = l.ServiceLogId
    AND s.StatusDetail = 'ExistingAccount'
)
  AND NOT EXISTS
(--Filter off logins where enrollment was Successfully Created
 --within 1 calendar day. 
  SELECT
    1
  FROM
    @logs AS s
  WHERE
    s.ServiceLogId = l.ServiceLogId
    AND s.StatusDetail = 'Successfully Created'
    AND CAST(l.TransactionDate AS DATE) = CAST(s.TransactionDate AS DATE)
);

结果:

+-------------------------+---------------+-----------+------------+--------------+
|     TransactionDate     | ServiceLogId  | EventName | EventType  | StatusDetail |
+-------------------------+---------------+-----------+------------+--------------+
| 2000-01-01 00:00:00.000 | ss@gmail.com  | LOGIN     | Enrollment | NextButton   |
| 2015-03-01 06:53:45.187 | abc@gmail.com | LOGIN     | Enrollment | NextButton   |
| 2015-03-05 06:53:45.187 | vrc@gmail.com | LOGIN     | Enrollment | NextButton   |
+-------------------------+---------------+-----------+------------+--------------+

编辑::添加了附加的WHERE条件,以仅选择任何给定用户的NextButton状态。

答案 1 :(得分:0)

只需从查询中排除服务日志数据

DECLARE @Logs TABLE
(
    Id int IDENTITY(1,1),
    TransactionDate     datetime,
    ServiceLogId    varchar(100),
    EventName   varchar(100),
    EventType   varchar(50),
    StatusDetail    varchar(50)
)

INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2000-01-01 00:00:00.000' , 'ss@gmail.com'   , 'LOGIN'     , 'Enrollment' , 'NextButton')
INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2015-05-12 06:53:02.957' , 'cvbb@gmail.com' , 'LOGIN'     , 'Enrollment' , 'NextButton')
INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2015-05-12 06:53:38.867' , 'cvbb@gmail.com' , 'LOGIN'     , 'Enrollment' , 'ExistingAccount')
INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2015-03-01 06:53:45.187' , 'abc@gmail.com' , 'LOGIN'     , 'Enrollment' , 'NextButton')
INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2015-03-05 06:53:45.187' , 'vrc@gmail.com'  , 'LOGIN'     , 'Enrollment' , 'NextButton')
INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2015-04-23 06:53:45.187' , 'sr@gmail.com'   , 'LOGIN'     , 'Enrollment' , 'NextButton')
INSERT INTO  @Logs(TransactionDate, ServiceLogId, EventName, EventType,StatusDetail)VALUES('2015-04-23 06:55:47.187' , 'sr@gmail.com'   , 'LOGIN'     , 'Enrollment' , 'Successfully Created' )

SELECT * FROM @Logs l
WHERE l.StatusDetail ='NextButton'
AND l.ServiceLogId NOT IN(SELECT ServiceLogId FROM @Logs l2 WHERE l2.StatusDetail IN ('ExistingAccount','Successfully Created' ))