如何仅显示可以';t登录并消除SQL中的其余部分

如何仅显示可以';t登录并消除SQL中的其余部分,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个包含以下列和数据的表,我正在尝试编写一个查询,以便它仅在用户未登录时显示: SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as verb union all SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as ver

我有一个包含以下列和数据的表,我正在尝试编写一个查询,以便它仅在用户未登录时显示:

SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'password reset' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'logged in' as verb
union all
SELECT 'Kevin' as First_name, 'Owens' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Kevin' as First_name, 'Owens' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Kevin' as First_name, 'Owens' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all                   
SELECT 'Kevin' as First_name, 'Owens' as Last_Name, getdate() as timestamp, 'password reset' as verb
union all                   
SELECT 'Kevin' as First_name, 'Owens' as Last_Name, getdate() as timestamp, 'logged in' as verb
union all
SELECT 'Kevin' as First_name, 'Owens' as Last_Name, getdate() as timestamp, 'logged in' as verb
union all
SELECT 'Roman' as First_name, 'Reigns' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all                   
SELECT 'Roman' as First_name, 'Reigns' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all                   
SELECT 'Roman' as First_name, 'Reigns' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Roman' as First_name, 'Reigns' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all                   
SELECT 'Roman' as First_name, 'Reigns' as Last_Name, getdate() as timestamp, 'password reset' as verb
union all
SELECT 'Roman' as First_name, 'Reigns' as Last_Name, getdate() as timestamp, 'password reset' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'password reset' as verb
union all
SELECT 'Seth' as First_name, 'Rollins' as Last_Name, getdate() as timestamp, 'tried log in' as verb
这为我提供了以下输出:

First_name  Last_Name   timestamp              verb
Seth        Rollins 2017-02-20 15:28:55.660 tried log in
Seth        Rollins 2017-02-20 15:28:55.660 tried log in
Seth        Rollins 2017-02-20 15:28:55.660 password reset
Seth        Rollins 2017-02-20 15:28:55.660 logged in
Kevin       Owens   2017-02-19 15:28:55.660 tried log in
Kevin       Owens   2017-02-19 15:28:55.660 tried log in
Kevin       Owens   2017-02-19 15:28:55.660 tried log in
Kevin       Owens   2017-02-20 15:28:55.660 password reset
Kevin       Owens   2017-02-20 15:28:55.660 logged in
Kevin       Owens   2017-02-21 15:28:55.660 logged in
Roman       Reigns  2017-02-20 15:28:55.660 tried log in
Roman       Reigns  2017-02-20 15:28:55.660 tried log in
Roman       Reigns  2017-02-20 15:28:55.660 tried log in
Roman       Reigns  2017-02-20 15:28:55.660 tried log in
Roman       Reigns  2017-02-20 15:28:55.660 password reset
Roman       Reigns  2017-02-20 15:28:55.660 password reset
Seth        Rollins 2017-02-21 15:28:55.660 tried log in
Seth        Rollins 2017-02-21 15:28:55.660 tried log in
Seth        Rollins 2017-02-21 15:28:55.660 tried log in
我目前正在尝试编写一个查询,因此它只显示Roman未成功登录时的统治次数,以便显示他尝试登录的次数以及密码重置。不应显示Seth Rollins的第一组结果,但应显示2017年2月21日的结果,因为他尚未成功登录

我目前正试图找出编写此查询的最佳方法

我的预期结果如下:

First_Name, Last_Name, Count of TriedLogIn, Count of PasswordResets
Roman        Reigns            4                        2
Seth         Rollins           3                       null

Seth Rollins名字出现的原因是,在成功登录20/02之后,他自该日期(21/02)起就没有成功登录过。成功登录后,计数将重置回0。

使用
不存在的

select *
from users u1
where verb = 'tried log in'
and not exists
(
select 1
from users u2
where u1.firstname = u2.firstname 
and u1.lastname = u2.lastname
and u2.verb = 'logged in'
)

对于该特定用户,使用
case
表达式进行条件计数:

select count(case when verb = 'tried log in' then 1 end) as loginfailures,
       count(case when verb = 'password reset' then 1 end) as passwordresets
from tablename
where First_name = 'Roman' and Last_name = 'Reigns'
如果您想要相同的信息,但对于所有用户,请按进行分组:

select First_name, Last_name,
       count(case when verb = 'tried log in' then 1 end) as loginfailures,
       count(case when verb = 'password reset' then 1 end) as passwordresets
from tablename
group by First_name, Last_name

谢谢你的回答,但我想这个问题不会起作用,因为赛斯·罗林斯在重置密码后第一次登录,但一天后遇到了困难。在您提到的代码中,这意味着2017年2月20日的尝试登录和密码重置也将被计入计数的一部分。希望这有点道理。@VGJ,你能编辑你的问题并添加预期的结果(格式文本)吗?谢谢你的回答,但这个查询不会像预期的那样工作-我也在问题中包含了预期的结果。