Hi YitzhakKhabinsky,
Thank you for the response. Usually I give DDL statement. here are the details:
DECLARE @Audit TABLE
(
ID smallint,
type varchar(100),
LoginDate datetime,
LoginUser varchar(7)
)
INSERT INTO @Audit (ID, type, LoginUser, LoginDate)
SELECT 1, 'Login', 'JQUA123', '2020-10-06 10:14:11.570'
UNION
SELECT 1, 'Logout - Session End', 'JQUA123', '2020-10-06 10:14:11.663'
UNION
SELECT 1, 'Login', 'JQUA123', '2020-10-06 10:36:08.460'
UNION
SELECT 1, 'Table 1 column1 update', 'JQUA123', '2020-10-06 10:37:12.000'
UNION
SELECT 1, 'Table 1 column2 update', 'JQUA123', '2020-10-06 10:37:12.000'
UNION
SELECT 1, 'Table 1 column3 update', 'JQUA123', '2020-10-06 10:37:12.000'
UNION
SELECT 1, 'Table 1 column4 update' , 'JQUA123', '2020-10-06 10:37:49.183'
UNION
SELECT 1, 'Table 1 column5 update', 'JQUA123', '2020-10-06 10:37:49.183'
UNION
SELECT 1, 'Table 1 column6 update', 'JQUA123', '2020-10-06 10:37:49.183'
UNION
SELECT 1, 'Table 1 column7 update', 'JQUA123', '2020-10-06 10:38:27.627'
UNION
SELECT 1, 'Table 1 column8 update', 'JQUA123', '2020-10-06 10:38:27.627'
UNION
SELECT 1, 'Table 1 column9 Insert', 'JQUA123', '2020-10-06 10:38:27.627'
UNION
SELECT 1, 'Logout - Session End', 'JQUA123', '2020-10-06 10:53:43.363'
UNION
SELECT 1, 'Login', 'JQUA123', '2020-10-07 10:58:38.403'
UNION
SELECT 1, 'Logout - Session End', 'JQUA123', '2020-10-07 10:58:38.497'
UNION
SELECT 2, 'Login', 'ABCD123', '2020-11-09 11:53:38.410'
UNION
SELECT 2, 'Table 1 column1 insert', 'ABCD123', '2020-11-09 11:55:07.783'
UNION
SELECT 2, 'Table 1 column2 insert', 'ABCD123', '2020-11-09 11:55:07.783'
UNION
SELECT 2, 'Table 1 column3 insert', 'ABCD123', '2020-11-09 11:55:07.783'
UNION
SELECT 2, 'Table 1 column4 insert' , 'ABCD123', '2020-11-09 11:55:07.783'
UNION
SELECT 2, 'Logout - Session End', 'ABCD123', '2020-11-09 11:55:07.785'
UNION
SELECT 3, 'Login', 'EFGH123', '2020-11-15 10:58:38.403'
UNION
SELECT 3, 'Logout - Session End', 'EFGH123', '2020-11-15 10:59:43.497'
--select * from @Audit
SELECT LoginUser, MAX(LoginDate) as LoginDate
FROM @Audit
WHERE type = 'Login'
GROUP BY LoginUser
ORDER BY Max(LoginDate) desc
--THis gives me unique user with max login date. In our example JQUA123 last login was on 7th.
Above select statement gives 3 rows.
LoginUser LoginDate
EFGH123 2020-11-15 10:58:38.403
ABCD123 2020-11-09 11:53:38.410
JQUA123 2020-10-07 10:58:38.403
-- now get the date of 30 days ago from current day
DECLARE @dt date = DATEADD(DAY,-30, CONVERT(Date,GETDATE())); -- which is 01/03/2021
-- I need a query that sets or updates login date for each row.
-- In this EFGH123 Login date will be updated with 2020-01-03 10:58:38.403 -- see only date part is updated to 30 days ago date
-- next row ABCD123 Login date should to update with a date which is 30 days to tomorrow. So, tomorrow another email will be sent to this user.
-- next row JQUA123 login date should be updated with a date which is 30 days from day after tomorrow
---So, that way every day there will a user with logindate of 30 days from that current day.
Output
LoginUser LoginDate New Update to LoginDate
EFGH123 2021-01-03 10:58:38.403 30 days from today (so update to 01/03/2021)
ABCD123 Don’t know what date it will be 11:53:38.410 Tomorrow this one should be 30 days (guessing 01/04/2021)
JQUA123 Don’t know what date it will be 10:58:38.403 Day after tomorrow this one should be 30 days
Thank You,