Comparing Date In Two Tables I don't think this is a stumper, but I'm having trouble because it involves aggregate functions.
I have a list of username's, user_id's in one table (users), and another table with user_id's, date's (board). The 'users' to 'board' relationship is 1 to many. In other words, the user_id is the unique key the users table, and every time a date is added to the 'board' table, a user_id is included. What I want to do is create a list of user_id's that have a date in the 'board' table that's less than 30 days old. So I want to take the MAX(date) from board, and compare it to getDate() and see if the result is less than 30, and if it is, include that user_id in my list. You don't really need to mess with the users table unless you want more information like the username, so the query might be similiar to this (although I know this is incorrect.)
SELECT user_id
FROM board
WHERE DATEDIFF(day, MAX(TableDate), getdate()) <= 30
GROUP BY user_id
However, this doesn't work... SQL complains about having the aggregate function MAX() in the WHERE clause. So I'm not sure how to write the clause to create my list. I'm actually trying to make a count of active users, so my query actually looks more like this, and might clue you into why I'm having difficulties.
SELECT count(user_id) AS activeCount
FROM board
WHERE DATEDIFF(day, MAX(TableDate), getdate()) <= 30
GROUP BY user_id
Thanks!
Jason
http://commerce.vertex.net/simple/clubs