pullingteeth
Programmer
I have the following tables (abbreviated):
User
----
username
firstname
lastname
role_code - describes the role of the user
Book_Event
----------
book_number
user
timestamp
event_code - describes the event
I wrote the following query to provide me with a list of users (within specific roles) who "have" books at a specific event (the fact that the book is at event 110 means that they "have" the book), but not beyond that event.
The query almost works. The problem is that as soon as a user has a book at a higher event, e.g. 120, they disappear from the results. This would seem to be a join related problem. However, the following slightly modified version(removing the count, and just listing the raw book_number) does not suffer from the same problem, leading me to think that the join is correct.
I would also like to list all users who have zero books at the event, with a 0 next to their name; however, this is a much lower priority than fixing the above problem.
Thanks!
User
----
username
firstname
lastname
role_code - describes the role of the user
Book_Event
----------
book_number
user
timestamp
event_code - describes the event
I wrote the following query to provide me with a list of users (within specific roles) who "have" books at a specific event (the fact that the book is at event 110 means that they "have" the book), but not beyond that event.
Code:
SELECT u1.firstname, u1.lastname, count(book_number)
FROM user AS u1 INNER JOIN book_Event ON u1.username = book_Event.user
WHERE ((book_Event.user) In
(SELECT u2.username
FROM user as u2
WHERE ((u2.role_code) Between 21 And 24)))
GROUP BY u1.firstname, u1.lastname
HAVING (MAX(event_code) = 110);
The query almost works. The problem is that as soon as a user has a book at a higher event, e.g. 120, they disappear from the results. This would seem to be a join related problem. However, the following slightly modified version(removing the count, and just listing the raw book_number) does not suffer from the same problem, leading me to think that the join is correct.
Code:
SELECT u1.firstname, u1.lastname, book_number
FROM user AS u1 INNER JOIN book_Event ON u1.username = book_Event.user
WHERE ((book_Event.user) In
(SELECT u2.username
FROM user as u2
WHERE ((u2.role_code) Between 21 And 24)))
GROUP BY u1.firstname, u1.lastname, book_number
HAVING (MAX(event_code) = 110);
I would also like to list all users who have zero books at the event, with a 0 next to their name; however, this is a much lower priority than fixing the above problem.
Thanks!