I've solved one issue and ran into another. Basicaly i want to select question_id, answer and maximum number of occurences. I run my query from basic table that gathers questions and answers to them (question id represents question and answer represents answer from 0 to 5 that corresponds to other table but that doesn't matter).
**survey_result**
question_id
answer (int from 0 to 5)
Sample survey_result:
question_id answer
1 3
1 5
1 2
2 2
2 0
2 4
Here's the query, it's purpose is to check for every single question, which answer (from 0 to 5) occured the most.
select question_id, answer, max(occurence_number) FROM
(select question_id, answer, count(*) as occurence_number
from survey_result
group by question_id, answer
order by question_id asc, occurence_number desc) as results
GROUP BY question_id
So a sub query results in something like this:
question_id answer occurence_number
1 0 12
1 1 20
1 2 34
1 3 5
1 4 9
1 5 15
But main query results something like this:
question_id answer occurence_number
1 0 12
2 0 20
3 0 34
4 0 5
So the problem is that it always shows answer 0, and i want to get correct answer number.
Sadly a bit redundant due to MySQL's lack of a WITH statement, but this should do what you want. In case of a tie, it will return the higher answer.
SELECT s1.question_id, MAX(s1.answer) answer, MAX(s1.c) occurrences
FROM
(SELECT question_id, answer, COUNT(*) c
FROM survey_result GROUP BY question_id,answer) s1
LEFT JOIN
(SELECT question_id, answer, COUNT(*) c
FROM survey_result GROUP BY question_id,answer) s2
ON s1.question_id=s2.question_id
AND s1.c < s2.c
WHERE s2.c IS NULL
GROUP BY question_id
An SQLfiddle to play with.
I think you are overcomplicating it, try this:
select question_id, answer, count(*) as occurence_number
from survey_result
group by question_id, answer
Related
Now I understand that this has been asked several times before, but I have tried to apply different existing solutions to my specific problems for quite a while without success. So I turn here in hope of some guidance.
I have a table called tblanswers, which contains answers linked to different questions in another table. What I want is to get the count for each answer for a specific question ID, but limit it to the n first answers each month.
Sample data from 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
This is the query I have so far:
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
That would give me something like this: (the dates and numbers in sample data is not accurate)
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
Let's say I only want to see the first three answers in a month, then count could never be more than 3. How can I limit each month?
The final data should be a sum of each answer, like this:
answer num_answers
1 2
2 3
3 3
I think one of these solutions could work, but not how:
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
Any help is appreciated. Thanks!
This solution is based on the top-N-per-group method 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
SQLFIDDLE
What about this solution:
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;
and the fiddle: http://sqlfiddle.com/#!2/1541eb/126
There is no reason to reinvent a wheel and risk you have a buggy, suboptimal code. Your problem is trivial extension of common per group limit problem (see also tag limit-per-group). There are already tested and optimized solutions to solve this problem.
I have a table include some data like that. This is example for question/answer script. ID is auto increment and if PID=0 then it is question, when reply any question then PID is set to question's ID. There is no Subject for replies.
ID PID SUBJECT CONTENT DATE
1 0 First Question This is my first 09/01/2013
2 0 Second Question This is second 09/01/2013
3 1 Yes this is first 09/01/2013
4 2 I agree this is second 10/01/2013
5 0 Third Question This is third question 11/01/2013
6 1 Reply to first 11/01/2013
7 1 Another reply to first 12/01/2013
8 5 This is reply of 5th 13/01/2013
9 2 Last try for second 14/01/2013
My questions are,
How can I select questions with reply count?
Ex.
First Question (3)
Second Question (2)
Third Question (1)
How can I select today's answered questions or answers?
Ex. For 09/01/2013
First Question (2) ---> 1 question and 1 answer but 2 actions
Second Question (1) ---> just 1 question
Select the questions and join against the answers:
select q.id, q.subject, count(a.id)
from yourtable q
left join yourtable a on q.id=a.pid
where q.pid=0
group by q.id;
Try join for first task
SELECT
q.id as ID,
q.pid as PID,
q.subject as SUBJECT,
COUNT(lq.id) as Total
FROM questions as q
LEFT JOIN questions as lq ON lq.pid = q.ID
WHERE q.PID = 0
GROUP BY q.id
OUTPUT
ID PID SUBJECT TOTAL
1 0 First Question 3
2 0 Second Question 2
5 0 Third Question 1
Demo
EDITS :
For second part. You should note that there can be many other ways for doing the same task.
SELECT
q.id as ID,
q.pid as PID,
q.subject as SUBJECT,
(COUNT(lq.id) - 1) as Total,
q.date
FROM questions as q
LEFT JOIN questions as lq ON lq.pid = q.ID OR lq.id = q.PID
WHERE q.date = DATE(NOW())
OUTPUT
ID PID SUBJECT TOTAL DATE
1 0 First Question 2 January, 09 2012 00:00:00+0000
2 0 Second Question 1 January, 09 2012 00:00:00+0000
Demo
For question 1
select PID,count(*) from table
where pid<>0
group by PID
For question 2
select PID,count(*) from table
where pid<>0 and date=current_date()
group by PID
I have a table that looks like this
id name answer question
1 john correct 1
1 john correct 2
1 john correct 3
1 john wrong 4
2 lee wrong 1
2 lee correct 2
2 lee correct 3
3 ana correct 1
3 ana wrong 2
I want to be able to get a list of all unique users, and see how many questions they have correct and how many are wrong.
I tried something like this but I cant seem to make it work:
SELECT id, user_id, name, question_id, (select count(answer) from table where answer = 'CORRECT') as correct, (select count(answer) from table where answer= 'WRONG') as wrong FROM table GROUP BY user_id
Can someone help me get me on the right track? thanks
Try this:
SELECT name, answer, COUNT(*) FROM yourtablenamehere GROUP BY name, answer
You do what you want with conditional sums:
select name, sum(case when answer = 'correct' then 1 else 0 end) as correct,
sum(case when answer = 'wrong' then 1 else 0 end) as wrong
from table t
group by name
for MySQL it could be like this:
SELECT user_id, name,SUM(IF(answer='correct',1,0)) AS correct,SUM(IF(answer='wrong',1,0)) as wrong FROM table GROUP BY user_id
Consider Facebook. Facebook displays the latest 2 comments of any status. I want to do something similar.
I have a table with e.g. status_id, comment_id, comment and timestamp.
Now I want to fetch the latest 2 comments for each status_id.
Currently I am first doing a GROUP_CONCAT of all columns, group by status_id and then taking the SUBSTRING_INDEX with -2.
This fetches the latest 2 comments, however the GROUP_CONCAT of all the records for a status_id is an overhead.
SELECT SUBSTRING_INDEX(GROUP_CONCAT('~', comment_id,
'~', comment,
'~', timestamp)
SEPARATOR '|~|'),
'|~|', -2)
FROM commenttable
GROUP BY status_id;
Can you help me with better approach?
My table looks like this -
status_id comment_id comment timestamp
1 1 xyz1 3 hour
1 2 xyz2 2 hour
1 3 xyz3 1 hour
2 4 xyz4 2 hour
2 6 xyz6 1 hour
3 5 xyz5 1 hour
So I want the output as -
1 2 xyz2 2 hour
1 3 xyz3 1 hour
2 4 xyz4 2 hour
2 6 xyz6 1 hour
3 5 xyz5 1 hour
Here is a great answer I came across here:
select status_id, comment_id, comment, timestamp
from commenttable
where (
select count(*) from commenttable as f
where f.status_id = commenttable.status_id
and f.timestamp < commenttable.timestamp
) <= 2;
This is not very efficient (O(n^2)) but it's a lot more efficient than concatenating strings and using substrings to isolate your desired result. Some would say that reverting to string operations instead of native database indexing robs you of the benefits of using a database in the first place.
After some struggle I found this solution -
The following gives me the row_id -
SELECT a.status_id,
a.comments_id,
COUNT(*) AS row_num
FROM comments a
JOIN comments b
ON a.status_id = b.status_id AND a.comments_id >= b.comments_id
GROUP BY a.status_id , a.comments_id
ORDER BY row_num DESC
The gives me the total rows -
SELECT com.status_id, COUNT(*) total
FROM comments com
GROUP BY com.status_id
In the where clause of the main select -
row_num = total OR row_num = total - 1
This gives the latest 2 rows. You can modify the where clause to fetch more than 2 latest rows.
I have 2 mysql tables
1. questions: with the following columns: id, title, answer1, answer2, answer3, answer4, answer5, nranswers.
and
2. answers with the following columns: id, questionid, userid, answer
Every question has maximum 5 answers( it can have between 2 and 5 answers). My problem is that I want to select from my database, for a given question, how many times was every option selected.
For example, let's suppose I have the question with the id 46, with 4 answers, and 48 users voted for the option #2, 37 users for the option #1 and 39 for the option #4.
I want a query that selects that and write these things:
1 37
2 48
3 0
4 39
P.S. VERY IMPORTANT! IT MUST COUNT ONLY NRANSWERS ANSWERS, AND IT MUST ECHO THE ONES THAT WEREN'T VOTED BEFORE.
Best way to do this: change table defs:
Questions (Question_ID, title)
Answers (Answer_ID, Question_ID, answer_text)
Votes (User_ID, Answer_ID)
Which contains the same data as your def, but is in first normal form. Selecting the counts is now really easy
SELECT
a.Answer_ID,
COUNT(v.User_ID)
FROM
Questions q
LEFT JOIN Answers a ON q.Question_ID = a.Question_ID
LEFT JOIN Votes v ON a.Answer_ID = v.Answer_ID
WHERE q.Question_ID = 46 -- or any other question ID
GROUP BY a.Answer_ID
ORDER BY a.Answer_ID;
SELECT q.id as question, a.answer as answer, count(a.answer) as count FROM questions q, answers a Group by q.id,a.answer
Problem with above that it will return as follows
question answer Count
1 1 37
1 2 48
1 4 39
1 3 0 this is missing
OR
SELECT a.question_id, a.answer, count(a.answer) FROM test.answers a Group by a.question_id, a.answer