sql query, finding inactive users for 1 week and 1 month

Hi!

i need to find inactive users for 1 week and for 1 month from Getdate().

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.

momi_sabagCommented:
for a week:
select custname, deviceId
from Customer t1
left outer join session t2
on t1.device_id = t2.device_id and t2.on_date_time < getdate() - 7

for a month:
select custname, deviceId
from Customer t1
left outer join session t2
on t1.device_id = t2.device_id and t2.on_date_time < dateadd(month, getdate(), -1)

if you want it in a single query:


select custname, deviceId,
 case when t2.device_id is null then 'Never logged in'
         when on_date_time > dateadd(month, getdate(), -1) then 'inactive for more than a month'
         when t2.on_date_time < getdate() - 7 then 'Inactive for more than a week' end
from Customer t1
left outer join session t2
on t1.device_id = t2.device_id
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Something like...

SELECT c.Custname, c.DeviseID
FROM Customer c
   LEFT JOIN (SELECT DeviceId, Max(Off_Date_time) as max_off_date_time
                      FROM Session 
                      GROUP BY DeviceId) s ON c.DeviceID = s.DeviceID 
   WHERE s.max_off_date_time < DATEADD(d, -7, GETDATE())

Open in new window


Change to DATEADD(m, -1, GETDATE()) for month
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
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.