Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sqlconfused
Yak Posting Veteran
50 Posts |
Posted - 2014-12-12 : 14:59:00
|
Hello.I have two tables. One is a list of users:USERSusername, userlevel------------John, 1Jim, 2John K, 3Ben, 3Ken, 2The other is a list of dates:GALLERYcrdate, creator-----------------2013-01-12, John2014-12-11, Jennifer2010-11-11, Jennifer2011-04-03, Ben2014-01-11, BenI'm trying to determine which users have the oldest crdates, that is, what users haven't added anything to the GALLERY table in a long time.My query is:select max(a.crdate) as last,a.creator from gallery a inner join users b on a.creator=b.usernamewhere userlevel >1 group by creatorI use the max(crdate) to obtain the highest date from the 'gallery' table and inner join it where the creator of the gallery = the username in the user database and has a userlevel greater than 1.This works, and will return Ben as:last / creator2011-04-03 / BenI'm trying to find only users who have galleries older than say, 180 days. This is causing a problem.select max(a.crdate) as last,a.creator from gallery a inner join users b on a.creator=b.usernameand b.userlevel >1 where a.crdate <= GETDATE() - 180 group by a.creatorWell that works, it returns only rows older than 180 days, from users with a userlevel > 1 *BUT* it will return columns older than 180 days even if that user has a newer date.Example: Ben will show as having a gallery for 2011-04-03 because he is userlevel>1 and the crdate is > 180 days. It ignores that Ben also has a newer column from 2014.So I need to select crdate older than 180 days but not if there are newer crdate's for that same user.Help? |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-12-12 : 15:05:24
|
Use the HAVING clause. HAVING max(a.crdate) < DATEADD(dd,-180,CAST(GETDATE() AS DATE)) HAVING CLAUSE comes after GROUP BY, but before ORDER BY.I didn't quite follow how you get the minimum date using the MAX function. Shouldn't you be using the MIN function instead? |
|
|
sqlconfused
Yak Posting Veteran
50 Posts |
Posted - 2014-12-12 : 15:19:49
|
Thanks for the reply. If I use the MIN function then I will only see the oldest minimum dates from all users. I want to find users that have not added content within 180 days but to obtain the LATEST (MAX) date that they did contribute.Example: You want to find which employee last used your employee benefits program, longer than 180 days ago. You'd not search the MIN date as that would be the earliest (very first time), you'd use MAX for the latest (newest) entry date he access your benefits.The answer you provided didn't work :(, it works but doesn't provide the result.a) older than 180 daysb) the newest date out of all the columns for that user (thus MAX) |
|
|
sqlconfused
Yak Posting Veteran
50 Posts |
Posted - 2014-12-12 : 15:59:35
|
Thanks for the reply. If I use the MIN function then I will only see the oldest minimum dates from all users. I want to find users that have not added content within 180 days but to obtain the LATEST (MAX) date that they did contribute.With your code I was able to get it working. |
|
|
|
|
|
|
|