declare @FromDate datetime; set @FromDate= dateadd(month,-3,getdate()); declare @toDate datetime; set @toDate = dateadd(month,8,getdate()); select * from AIAUser where FirstName like'%Khalid%' SELECT LoginDetail.UserId, COUNT(1) as TotalLogins, MAX(LoginDetail.LoginTime) FROM LoginDetail WHERE (LoginTime) BETWEEN @FromDate AND @toDate AND UserId = 32462 GROUP BY LoginDetail.UserId