A SQL application problem encountered at work

Source: Internet
Author: User
Tags getdate

Log in Log table

CREATE TABLE [dbo]. [Loginlog]
(
[Seq] [INT] Not NULL IDENTITY (1, 1),--seq
[UserId] [varchar] (COLLATE) Chinese_prc_ci_as NULL,--User name
[Logintime] [DateTime] NULL,--Logon time
[SUCCESSFLG] [Bit] NULL,--whether the login was successful

) on [PRIMARY]
GO

Requirement: Returns whether the user can continue to log in

Login rules: If the user logs in three times within 1 hours of error, the lock cannot be logged in for 1 days

Implementing SQL

Select Case is COUNT (1) = 3 then 1 ELSE 0 END lockflgfrom (select TOP 3 l.* from Loginlog L with (nolock) INNER JOIN (Sele CT TOP 1 seq,successflg,logintime,userid from dbo. Loginlog with (nolock) WHERE userid= ' sdf333 ' and Logintime >= DATEADD (Day,-1,getdate ()) ORDER by Seq DESC) Lastlog on l.se Q  <= lastlog.seq and L.userid = Lastlog.useridwhere  DATEDIFF (HOUR, L.logintime,lastlog.logintime) < 1ORDER by L.SEQ  desc) as Twhere T.SUCCESSFLG = 0

A newcomer to the company is very interested in SQL. Let him try. Given SQL:

Select  COUNT (1) lockflg from (Select Case is COUNT (*) =3 then 1 ELSE 0 END as Near3,case when DATEDIFF (Hour,min (t.Log InTime), MAX (t.logintime)) <1 then 1 else 0 END as Inonehour,case if SUM (CAST (t.successflg as INT)) =0 then 1 else 0 EN D as Wrongtimes,case when DATEDIFF (Hour,max (T.logintime), GETDATE ()) <24 then 1 ELSE 0 END as Inonedayfrom (SELECT TOP 3 * FROM dbo. Loginlogwhere userid= ' sdf333 ' ORDER by Logintime DESC) t) Ttwhere Near3=1 and Inonehour=1 and Wrongtimes=1 and Inoneday=1

This classmate's idea is more simple and direct, feel I am already old ~ ~ ~

A SQL application problem encountered at work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.