user2799097
user2799097

Reputation: 31

Select one specfic row from each user by a different time

I want to select the last answer for a specific question. My current solution selects only the Question from "Nutzer1237", but i want the last Answer from "Nutzer1234" too.

enter image description here

SELECT pid, frage, antwort, user, created_at
FROM antwortenverlauf
WHERE frage =  'Risiko: Wie empfinden Sie die Kommunikation mit dem Kunden?'
AND (
user, created_at
)
IN (

SELECT user, MAX( created_at ) 
FROM antwortenverlauf
)
ORDER BY created_at DESC 

Sorry for my English!

Translation: frage = question

antwort = answer

Upvotes: 0

Views: 75

Answers (4)

Alberto Solano
Alberto Solano

Reputation: 8227

Your query works as expected giving the record with Nutzer1237 as 'user', because, in your subquery, you're looking for the records with the most recent timestamp: MAX( created_at ) without grouping your results per user.

You have then to group the results per user in your subquery, using this query:

SELECT user, MAX( created_at ) 
FROM antwortenverlauf
GROUP BY user

Note: Make sure also that the value of the frage column you're looking for doesn't contain undesired spaces or other characters.

However, your query won't get your desired results because you're looking for the MAX(created_at) without considering the frage column. You have to move the WHERE condition in your subquery. Your final query will then look like this:

SELECT pid, frage, antwort, user, created_at
FROM antwortenverlauf
WHERE (user, created_at)
IN (    
SELECT user, MAX( created_at ) 
FROM antwortenverlauf
WHERE frage =  'Risiko: Wie empfinden Sie die Kommunikation mit dem Kunden?'
GROUP BY user
)
ORDER BY created_at DESC 

Upvotes: 1

niyou
niyou

Reputation: 873

you should have read the answer of @fancyPants

SELECT pid, frage, antwort, user, created_at
FROM antwortenverlauf a1
WHERE frage =  'Risiko: Wie empfinden Sie die Kommunikation mit dem Kunden?'
    AND (user, created_at) IN (SELECT user, MAX( created_at ) 
                               FROM antwortenverlauf a2
                               WHERE a1.frage=a2.frage 
                               GROUP BY USER)
ORDER BY created_at DESC

Upvotes: 1

Lijo
Lijo

Reputation: 6788

SELECT pid, frage, antwort, user, created_at
FROM antwortenverlauf
WHERE frage =  'Risiko: Wie empfinden Sie die Kommunikation mit dem Kunden?'
AND (
user, created_at
)
IN (
SELECT user, MAX( created_at ) 
FROM antwortenverlauf

GROUP BY USER               // **this is the only difference**
)
ORDER BY created_at DESC 

in your code the inner query only return one raw that was the problem

enter image description here

Upvotes: 0

fancyPants
fancyPants

Reputation: 51908

In the official manual there are 3 examples how to solve this.

Task: For each article, find the dealer or dealers with the most expensive price.

This problem can be solved with a subquery like this one:

SELECT article, dealer, price
FROM   shop s1
WHERE  price=(SELECT MAX(s2.price)
              FROM shop s2
              WHERE s1.article = s2.article);

The preceding example uses a correlated subquery, which can be inefficient (see Section 13.2.10.7, “Correlated Subqueries”). Other possibilities for solving the problem are to use an uncorrelated subquery in the FROM clause or a LEFT JOIN.

Uncorrelated subquery:

SELECT s1.article, dealer, s1.price
FROM shop s1
JOIN (
  SELECT article, MAX(price) AS price
  FROM shop
  GROUP BY article) AS s2
  ON s1.article = s2.article AND s1.price = s2.price;

LEFT JOIN:

SELECT s1.article, s1.dealer, s1.price
FROM shop s1
LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price
WHERE s2.article IS NULL;

The LEFT JOIN works on the basis that when s1.price is at its maximum value, there is no s2.price with a greater value and the s2 rows values will be NULL.

Upvotes: 1

Related Questions