Find the distinct user where he has logged in all the months from the given date has to be the current date, not consider the month.
Only the current date. Not current month
Find the distinct users who logged I every month
From the example, Aditya and Kumar logged in Jan. Feb, Mar, Apr so they are called as repeated users
So I need count as 2
New User: If a user logged in only only the latest week and never before is called as new user
Ex: Gopal is called as a new user as he logged only on April 26 and 28
CREATE TABLE [dbo].[dbo.usagetracker]( [SNo] [nchar](10) NULL, [Username] [nvarchar](50) NULL, [LoginDate] [datetime2](7) NULL ) ON [PRIMARY] INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'1 ', N'Adhitya', CAST(N'2020-01-12T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'2 ', N'Selvam', CAST(N'2020-01-01T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'3 ', N'Kumar', CAST(N'2020-02-02T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'4 ', N'Adhitya', CAST(N'2020-02-02T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'5 ', N'Selvam', CAST(N'2020-02-12T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'6 ', N'Kumar', CAST(N'2020-02-02T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'7 ', N'Adhitya', CAST(N'2020-03-17T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'8 ', N'Selvam', CAST(N'2020-03-23T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'9 ', N'Kumar', CAST(N'2020-03-23T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'10', N'Kumar', CAST(N'2020-03-27T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'11', N'Kumar', CAST(N'2020-04-02T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'12', N'Kumar', CAST(N'2020-04-15T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'13', N' pal', CAST(N'2020-04-26T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'14', N' pal', CAST(N'2020-04-28T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'15', N'Adhitya', CAST(N'2020-04-28T00:00:00.0000000' AS DateTime2)) INSERT [dbo].[dbo.usagetracker] ([SNo], [Username], [LoginDate]) VALUES (N'16', N'Kumar', CAST(N'2020-01-15T00:00:00.0000000' AS DateTime2))
You can use aggregation and a having
clause:
select username
from t
where logindate >= @date
group by username
having count(distinct year(logindate) * 100 + month(logindate)) = datediff(month, @date, logindate) + 1;
Actually, you can simplify that having
clause to:
having count(distinct eomonth(logindate)) = datediff(month, @date, logindate) + 1;
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.