I've got the following line in a stored procedure when someone is logging into his account:
UPDATE AcccountsTable SET LogDate = getdate() WHERE AccID = @identity
However, I want to add a new feature where on every consecutive login day to add +1
to a new column ( ConsecutiveDays
) in my AccountsTable
.
For example:
Day 1: 2018-04-11 20:00
Day 2: 2018-04-12 15:30
Day 3: 2018-04-13 10:20
and etc ....
Total consecutive days should be 3 days for the user account id that logged during these consecutive days.
If the user doesn't log on day 4
- 2018-04-14
but logs on the next day 2018-04-15
then all consecutive days should be reverted back to 1
.
I've searched for a similar solution for a stored procedure on MSSQL Server 2008 R2 but without success. I am not good at SQL at all, so I will appreciate your help a lot! Thank you in advance!
That shouldn't be too hard using case
:
UPDATE AcccountsTable
SET ConsecutiveDays =
CASE WHEN CAST(LogDate as Date) = CAST(DATEADD(DAY, -1, GETDATE()) As Date)
AND ConsecutiveDays < 28 THEN
ConsecutiveDays + 1
WHEN CAST(LogDate as Date) = CAST(GETDATE() as Date) THEN
ConsecutiveDays
ELSE
1
END,
ChallengesCompleted =
CASE WHEN ConsecutiveDays = 28
AND CAST(LogDate as Date) = CAST(DATEADD(DAY, -1, GETDATE()) As Date)
THEN
ChallengesCompleted + 1
ELSE
ChallengesCompleted
END,
LogDate = GETDATE()
WHERE AccID = @identity
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.