现在我明白以前曾经多次询问过这个问题,但是我试图将不同的现有解决方案应用于我的具体问题已经有一段时间没有成功。所以我转到这里希望得到一些指导。
我有一个名为tblanswers的表,其中包含与另一个表中不同问题相关联的答案。 我想要的是获取特定问题ID的每个答案的计数,但将其限制为每月的第一个答案。
来自tblanswers的示例数据:
id qid answer timestamp
72 162 2 1366027324
71 161 4 1343599200
70 162 2 1366014201
69 161 4 1366011700
68 162 2 1366006729
67 161 3 1366010948
66 162 2 1365951084
这是我到目前为止的查询:
SELECT *, COUNT(*) c FROM(
SELECT answer, timestamp, YEAR(FROM_UNIXTIME(timestamp)) yr, MONTH(FROM_UNIXTIME(timestamp)) mo FROM tblanswers
WHERE qid = 161
ORDER BY timestamp ASC
) q GROUP BY YEAR(FROM_UNIXTIME(timestamp)), MONTH(FROM_UNIXTIME(timestamp)), answer
这会给我这样的事情:(样本数据中的日期和数字不准确)
answer yr mo c
1 2013 5 5
2 2013 5 3
3 2013 5 2
1 2013 6 5
2 2013 6 15
3 2013 6 7
假设我只想在一个月内看到前三个答案,那么计数永远不会超过3.我怎样才能限制每个月?
最终数据应该是每个答案的总和,如下所示:
answer num_answers
1 2
2 3
3 3
我认为其中一种解决方案可行,但不是如何: http://code.openark.org/blog/mysql/sql-selecting-top-n-records-per-group http://code.openark.org/blog/mysql/sql-selecting-top-n-records-per-group-another-solution
感谢任何帮助。谢谢!
答案 0 :(得分:0)
此解决方案基于每组前N个方法here
SELECT answer, COUNT(*) num_answers
FROM (SELECT answer, yearmonth,
@rn := CASE WHEN @prevmonth = yearmonth
THEN @rn + 1
ELSE 1
END rn,
@prevmonth := yearmonth
FROM (SELECT @rn := NULL, @prevmonth := NULL) init,
(SELECT answer,
YEAR(FROM_UNIXTIME(timestamp))*100+MONTH(FROM_UNIXTIME(timestamp)) yearmonth
FROM tblanswers
WHERE qid = 220
ORDER BY timestamp) x) y
WHERE rn <= 3
GROUP BY answer
答案 1 :(得分:0)
这个解决方案怎么样:
SELECT qid, answer, YEAR(FROM_UNIXTIME(timestamp)) yr, MONTH(FROM_UNIXTIME(timestamp)) mo, COUNT(*) no
FROM tblanswers
WHERE qid = 161
GROUP BY answer, yr, mo
HAVING COUNT(*) <= 2
ORDER BY timestamp ASC;
答案 2 :(得分:-1)
没有理由重新发明轮子,并冒险你有一个错误的,次优的代码。您的问题是常见per group limit problem的简单扩展(另请参见标记limit-per-group)。已有tested and optimized solutions to solve this problem。