Solved

Selecting the latest answers only per user from survey DB

Posted on 2010-09-10
12
299 Views
Last Modified: 2013-12-13
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?
0
Comment
Question by:freestate
  • 3
  • 3
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33644464
Try:

SELECT
      MAX(a2.time),
      question,
      answer,
      user
FROM answers a1
GROUP BY user, question , answer
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33644526
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
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 250 total points
ID: 33644627
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33645080
[
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
 

Author Closing Comment

by:freestate
ID: 33645142
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33645290
>> 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
 

Author Comment

by:freestate
ID: 33645358
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
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 33645380
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
 
LVL 14

Expert Comment

by:ddsh79
ID: 33645716
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
 

Author Comment

by:freestate
ID: 33645764
Fair point - although for my purposes, entries are stored sequentially with an auto-generated incrementing ID, so it suits purpose.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
The viewer will learn how to dynamically set the form action using jQuery.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now