Link to home
Start Free TrialLog in
Avatar of jamppi
jamppiFlag for Sweden

asked on

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
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

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'.
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
Avatar of jamppi

ASKER

Hi!

Jimhorn:
it will not work. 'multipart identifier could not be found'
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jamppi

ASKER

Perfect!

Thank You.
Thanks for the grade.  Good luck with your project.  -Jim