Selecting the latest answers only per user from survey DB

I have a database table that contains answers from a survey, stored on a per user database with the following columns:

ID
User
Question
Answer
Time

where time stores the current timestamp when the question is answered.

As this survey can be completed more than once, I need to be able to return records on a per use basis that ONLY give the latest answer to each question.  (I also need to return the 'earliest' at some point in the future, but I guess this will just require swapping Max for Min)

I've tried this, but it returns a list of all answers:

SELECT time, question, answer, user FROM answers a1 WHERE user = 8 AND time=(SELECT MAX(a2.time) FROM answers a2 WHERE a1.id = a2.id AND user = 8)

I also tried this, but it only seems to give the Max sate, not the associated answer:

SELECT MAX( time ) , question, answer, user
FROM answers
WHERE user =8
GROUP BY question

Can someone help me?
freestateAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
Hi,

You are right, JoeNuvo in http:#a33644627 almost had it right, but was grouping only by Question. I went too far with answer.
It should just be as you have shown in http:#a33645358
0
 
Om PrakashCommented:
Try:

SELECT
      MAX(a2.time),
      question,
      answer,
      user
FROM answers a1
GROUP BY user, question , answer
0
 
Dushyant SharmaCommented:
Your first query will return every row for user 8 as you are matching id inside the subquery. i believe you want to do this
SELECT time, question, answer, user FROM answers a1 WHERE user = 8 AND time=(SELECT MAX(a2.time) FROM answers a2 where a1.question=a2.question and user = 8)

The second query returns the desired result i think.
SELECT MAX( time ) , question, answer, user
FROM answers
WHERE user =8
GROUP BY question;

The output i receive on test data is the latest answer for distinct questions for user 8
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
JoeNuvoConnect With a Mentor Commented:
if your ID is autonumber/unique value

here is the query to give you a list of lastest answer for each question

SELECT * FROM [answers]
WHERE ID IN
(
SELECT MAX(ID) As ID
FROM [answers]
WHERE user = 8
GROUP BY Question
)

but if ID is not autonumber/unique value

try this query

SELECT [answer].*
FROM [answers]
INNER JOIN
(
SELECT MAX([Time]) As [Time], Question
FROM [answers]
WHERE user = 8
GROUP BY Question
) As SubQuery
ON [answers].[Time] = SubQuery.[Time] AND [answers].Question = SubQuery.Question
WHERE User = 8
0
 
cyberkiwiCommented:
[
SELECT MAX( time ) , question, answer, user
FROM answers
WHERE user =8
GROUP BY question
]
does not work because answer and user are neither in GROUP BY nor aggregates. Any random answer and user will be returned, not the ones matching MAX(time)

I believe this is what you are after.

SELECT time, question, answer, user
FROM answers
where ID in
(
      select max(id) from answers
      group by user, question, answer
)
0
 
freestateAuthor Commented:
Doh  -you're right, the second one works.  Thanks for explaining why, because I was just shooting in the dark to get this to work.  
0
 
cyberkiwiCommented:
>> The output i receive on test data is the latest answer for distinct questions for user 8

The accepted answer is not correct.  It doesn't always work.
See http:#a33645080

http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html
http://blog.mclaughlinsoftware.com/2010/03/10/mysql-standard-group-by/
0
 
freestateAuthor Commented:
Cyberwiki - you're right - how can I reopen the question?

Your solution seems almost there, but I get duplicate answers where a different answer has been provided to the same question at different times.

Am I right in thinking that this solves this problem (it seems to):

SELECT time, question, answer, user
FROM answers
where ID in
(
      select max(id) from answers
      group by user, question
)

(i.e. drop 'answers' from the group by clause)
0
 
Dushyant SharmaCommented:
The below query makes no sense in selecting the latest time max(id).. You are selecting the latest entry by ID not by time. I have inserted few sample data in the structure provided by you. It works fine if i enter the data in sequence. But it breaks when i update the Time and make any random entry latest by time.

SELECT time, question, answer, user
FROM answers
where ID in
(
      select max(id) from answers
      group by user, question
)
0
 
freestateAuthor Commented:
Fair point - although for my purposes, entries are stored sequentially with an auto-generated incrementing ID, so it suits purpose.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.