简体   繁体   English

在不同的时间从每个用户中选择一个特定的行

[英]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. 我当前的解决方案仅从“ Nutzer1237”中选择问题,但我也希望从“ Nutzer1234”中获得最后一个答案。

在此处输入图片说明

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 翻译:frage =问题

antwort = answer antwort =答案

In the official manual there are 3 examples how to solve this. 官方手册中 ,有3个如何解决此问题的示例。

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”). 前面的示例使用了一个关联子查询,该子查询可能效率不高(请参见第13.2.10.7节“关联子查询”)。 Other possibilities for solving the problem are to use an uncorrelated subquery in the FROM clause or a LEFT JOIN. 解决该问题的其他可能性是在FROM子句或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. LEFT JOIN的工作原理是,当s1.price达到最大值时,不存在更大的s2.price且s2行的值将为NULL。

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 . 您的查询按预期方式工作,以Nutzer1237作为“用户”给出记录,因为在子查询中,您正在查找具有最近时间戳记的记录: MAX( created_at )而不将每个user的结果分组。

You have then to group the results per user in your subquery, using this query: 然后,您必须使用以下查询在子查询中将每个user的结果分组:

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. 注意:还请确保您要查找的frage列的值不包含不希望的空格或其他字符。

However, your query won't get your desired results because you're looking for the MAX(created_at) without considering the frage column. 但是,您的查询不会得到您想要的结果,因为您在查找MAX(created_at)未考虑frage列。 You have to move the WHERE condition in your subquery. 您必须在子查询中移动WHERE条件。 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 

you should have read the answer of @fancyPants 您应该已经阅读@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
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 在您的代码中,内部查询仅返回一个原始问题

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM