如何根据另一个日期列计算日期列?

时间:2016-06-03 22:06:28

标签: sql sql-server count distinct

我想要实现的是根据LoginAttempts值和LastLoginDate计算用户的登录尝试次数。例如,我需要在30天内使用2次登录尝试查询LastLoginDate。

sampledb

结果应该是: Output

我拥有的是这个..我创建了临时表来提取信息,并且它似乎没有正确计数。这就是我被困住的地方。任何帮助都会受到赞赏!!

sample database

3 个答案:

答案 0 :(得分:2)

您的GROUP BY不正确。它包括LastLoginDateUTC。因此,您需要计算每个日期的登录次数,而不是每30天登录一次。

从GROUP BY中删除LastLoginDateUTC,并将SELECT子句更改为使用plt.imshow(acov,interpolation='nearest', cmap='Reds') plt.colorbar() plt.show() 。这应该给你你想要的。

答案 1 :(得分:0)

我不确定我是否完全理解您的请求,但这里的内容已经不在了。祝你好运!:

   select FirstName, StudentID, UserName, LastLoginDate, LoginAttempts,
    RowNumber = row_number() over(partition by StudentID order by StudentID, LoginAttempts)
    into #temp1
    from user
    where cast(LastLoginDate as date) >= getdate()- 30
    --should return all rows of data for past 30 days. 
    --should also rank each loginAttempt by student 

    select * from #temp1
    where RowNumber >= 3

答案 2 :(得分:0)

这不是答案......更多建议。

我认为你有学生,你需要审核登录尝试 这对我来说是一对多的关系 所以我会保留学生表,从任何登录相关数据中删除 这样可以简化您的Student表,减少行数,节省存储空间,不会反复使用相同的数据(名称,用户名等)。
该数据将在StudentLoginAttempts中,例如:

    Create Table StudentLoginAttempts (
        Id int not null identity(1,1),
        StudentId int not null,
        LoginDate datetime not null,
        Successful bit not null,

        Constraint PK_StudentLoginAttempts Primary Key Clustered (Id),
        Constraint FK_StudentLoginAttempts_Student Foreign Key (StudentId) References Student(StudentId)
    )
    go

    Create Index IX_StudentLoginAttempts_StudentId On StudentLoginAttempts(StudentId)
    go

    Create Index IX_StudentLoginAttempts_LoginDate On StudentLoginAttempts(LoginDate)
    go

所以事情可能会更清楚,你可以获得更多信息 想想下面的例子:

Create Table #Student (
    StudentId int not null identity(1,1),
    Username varchar(50) not null,
    FirstName varchar(50) not null
)

Create Table #StudentLoginAttempts (
    Id int not null identity(1,1),
    StudentId int not null,
    LoginDate datetime not null,
    Successful bit not null
)

insert into #Student values
( 'Student001', 'JON' ),
( 'Student002', 'STEVE' )

insert into #StudentLoginAttempts values
( 1, '2016-01-01 09:12', 0 ),
( 1, '2016-02-01 09:12', 0 ),
( 1, '2016-03-01 09:12', 1 ),
( 2, '2016-03-02 10:12', 0 ),
( 2, '2016-04-02 10:12', 1 ),
( 2, '2016-05-02 10:12', 0 )

;with TotalAttemptsCte as (
    select StudentId, TotalLoginAttempts = count(*) from #StudentLoginAttempts group by StudentId
),
FailedCte as (
    select StudentId, FailedLogins = count(*) from #StudentLoginAttempts where ( Successful = 0 ) group by StudentId
),
SuccessfulCte as (
    select StudentId, SuccessfulLogins = count(*) from #StudentLoginAttempts where ( Successful = 1 ) group by StudentId
),
LastSuccessFulDateCte as (
    select StudentId, max(LoginDate) as LastSuccessfulLoginDate
    from
        #StudentLoginAttempts
    where
        ( Successful = 1 ) 
    group by StudentId
)
select
    a.*, b.TotalLoginAttempts, c.FailedLogins, d.SuccessfulLogins, e.LastSuccessfulLoginDate
from
    #Student a
    left join TotalAttemptsCte b       on ( a.StudentId = b.StudentId )
    left join FailedCte c              on ( a.StudentId = c.StudentId )
    left join SuccessfulCte d          on ( a.StudentId = d.StudentId )
    left join LastSuccessFulDateCte e  on ( a.StudentId = e.StudentId )

Drop Table #StudentLoginAttempts
Drop Table #Student

您还可以根据查询创建视图,以便更轻松地访问。

我提醒你,这只是一个建议,我会怎么做。

相关问题