Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selecting the latest answers only per user from survey DB

Posted on 2010-09-10
12
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Dushyant Sharma
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 1000 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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
 
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 1000 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:Dushyant Sharma
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

610 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