sql query, finding users who logged in less than 5 times per month / 10 times per 3 months

Hi !

i need to find users  logged-in less then 5 times per month and less then 10 times per 3 months.

Table layouts.

Customer table.
DeviceId, Custname,,,,,,,,,,,,

session table
Sessions_Id, DeviceId, on_date_time, Off_Date_Time, Complete,,,,,,,,,,,,,

need it to output

custname  deviceId
jamppiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
if 'per month' means the latest month, then give this a whirl..
SELECT s.DeviceID, c.custName
FROM (
	SELECT DeviceId, COUNT(DeviceID) as the_count
	FROM Session 
	WHERE Off_Date_Time > DATEADD(m, -1, GETDATE()) 
	HAVING COUNT(DeviceID) < 5 ) s
	JOIN Customer c ON s.DeviceID = c.DeviceID

Open in new window

Replace -1 and 5 with -3 and 10 for 'less than 10 times per 3 months'.
0
Brian CroweDatabase AdministratorCommented:
I just spent 10 minutes writing a cte query that would do this but experts-exchange threw it away because I wasn't already logged in.

sry
0
jamppiAuthor Commented:
Hi!

Jimhorn:
it will not work. 'multipart identifier could not be found'
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>it will not work. 'multipart identifier could not be found'
Doh.  Forgot the GROUP BY.
SELECT s.DeviceID, c.custName
FROM (
	SELECT DeviceId, COUNT(DeviceID) as the_count
	FROM Session 
	WHERE Off_Date_Time > DATEADD(m, -1, GETDATE()) 
	GROUP BY DeviceId
                  HAVING COUNT(DeviceID) < 5 ) s
	JOIN Customer c ON s.DeviceID = c.DeviceID 

Open in new window


>I just spent 10 minutes ... but experts-exchange threw it away because I wasn't already logged in.
That's been happening to me too lately.  What the heck..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jamppiAuthor Commented:
Perfect!

Thank You.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.