在SQL Server表查询中查找不同的用户
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在SQL Server表查询中查找不同的用户相关的知识,希望对你有一定的参考价值。
找到给定日期以来所有月份的唯一身份用户必须是当前日期,而不考虑月份。
仅当前日期。不是当前月份
查找每月登录我的不同用户
从示例中,Aditya和Kumar登录于1月,2月,3月,4月,因此被称为重复用户
所以我需要算为2
[新用户:如果只在最近一周登录的用户被称为新用户
例如:Gopal被称为新用户,因为他仅在4月26日和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))
答案
您可以使用聚合和having
子句:
select username
from t
where logindate >= @date
group by username
having count(distinct year(logindate) * 100 + month(logindate)) = datediff(month, @date, logindate) + 1;
实际上,您可以将having
子句简化为:
having count(distinct eomonth(logindate)) = datediff(month, @date, logindate) + 1;
以上是关于在SQL Server表查询中查找不同的用户的主要内容,如果未能解决你的问题,请参考以下文章