views:

39

answers:

1

**Update: Title should have read: Sql Query - Unique item with latest entry

Hi there,

I have a two tables. users and rsvp.
Users has user detail in it (id, name, email)
rsvp has rsvp status in it (answerid, userid, eventid, answer)

Note:
answer 1 = coming
answer 2 = not coming
answer 3 = maybe
answer 4 (pseudo answer) = no reply

Please note that "answer 4" never ever appears in the actual rsvp table data. It is constructed on the fly with the below SQL

A query to see whos coming, whos not, whos maybe coming AND who has not responded looks as follows:

 SELECT
  usrs.name,
  usrs.email,

  (SELECT CASE 
  WHEN rsvp.answer = 1 THEN 'Yes, see you there' 
  WHEN rsvp.answer = 2 THEN 'Cannot make it' 
  WHEN rsvp.answer = 4 THEN 'Cannot make it' 
  WHEN rsvp.answer = 3 THEN 'Maybe'
  ELSE 'No Reply' END) as attendance,

  (SELECT CASE 
  WHEN rsvp.answer = 1 THEN "1" 
  WHEN rsvp.answer = 2 THEN "3"
  WHEN rsvp.answer = 4 THEN "4"
  WHEN rsvp.answer = 3 THEN "2"
  ELSE "5" END) as ordering

  FROM jos_users usrs
  LEFT JOIN jos_rsvp as rsvp on usrs.id = rsvp.userid
  WHERE usrs.id NOT IN (62,63,128)
  ORDER BY ordering

The same user may reply more than once. I.e. First they reply YES and then later they change their mind and reply NO.

rsvp answers are not updated, a new record is simply inserted for the new answer. The problem with the SQL above is that it does not take this duplication into account.

I'm trying to figure out how to change it so that only the last answer for each user is shown in the results. It seemed simple at first, but now I cant figure it out.

Using MySQL.

Please Help,
Losing Hair
n4~

+1  A: 

How about this?

 SELECT
  usrs.name,
  usrs.email,

  (SELECT CASE 
  WHEN rsvp.answer = 1 THEN 'Yes, see you there' 
  WHEN rsvp.answer = 2 THEN 'Cannot make it' 
  WHEN rsvp.answer = 4 THEN 'Cannot make it' 
  WHEN rsvp.answer = 3 THEN 'Maybe'
  ELSE 'No Reply' END) as attendance,

  (SELECT CASE 
  WHEN rsvp.answer = 1 THEN "1" 
  WHEN rsvp.answer = 2 THEN "3"
  WHEN rsvp.answer = 4 THEN "4"
  WHEN rsvp.answer = 3 THEN "2"
  ELSE "5" END) as ordering

  FROM jos_users usrs, jos_rsvp rsvp,
  (SELECT userid, max(answerid) AS latest_answerid
   FROM jos_rsvp
   WHERE userid NOT IN (62,63,128)
   GROUP BY userid
  ) AS latest_rsvp
  WHERE usrs.id = rsvp.userid
  AND rsvp.userid = latest_rsvp.userid
  AND rsvp.answerid = latest_rsvp.latest_answerid
  ORDER BY ordering
eumiro
That's works great eumiro. That does eliminate the duplicate and gives me the latest rsvp, but now we only have yes no and maybe's in our report. What about the people who have not replied yet and do not have an answer entry in the rsvp table?
n4rzul
I have eliminated your `LEFT JOIN`. Maybe try to replace my `FROM...`line with `FROM jos_users usrs LEFT JOIN jos_rsvp rsvp ON usrs.id = rsvp.userid` and remove my `[WHERE] usrs.id = rsvp.userid AND` below.
eumiro
Nope, has no effect.
n4rzul
For now I think I'm just going to seperate the two queries. One for the yes no and maybes and a different one for the ones who haven't replied yet. Thank you eumiro. You have been most helpfull.
n4rzul
Good luck! It is easier to test different possibilities if you have the database with the tables in your hands.
eumiro