How to select 2 distinct columns and count one of them - mysql

Ok so I'm trying to do the following. I have a table that has questions and answers. I'm trying to figure out how many answers I have of each answer on each question.
Question Answer
-------- ------
What is your favorite color? Blue
What is your favorite color? Red
What is your favorite color? Blue
What is your age? 12
What is your age? 15
Now what I want is results (query) to output something like this:
What is your favorite color? Blue 2
What is your favorite color? Red 1
What is your age? 12 1
What is your age? 15 1
OR
What is your favorite color? 2 1
What is your age? 1 1
In the last part...I would know what those values mean based on the query.

SELECT t.Question,
t.Answer,
COUNT(*) AS 'Count'
FROM YourTable AS t
GROUP BY t.Question,
t.Answer

The answer to the second question involves count(distinct):
select t.Question, count(distinct t.Answer) as NumAnswers,
count(*) as NumAnswered
from table t
group by t.Question;

Related

Need advice on SELECT query with multiple rows that should become columns [duplicate]

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 4 years ago.
I have a table:
id data-type data-answer
-----------------------------------
1 car honda
1 color yellow
1 engine gasoline
2 car bmw
2 color black
3 engine diesel
Need advice, how to write SELECT so that it would be in output:
id car color engine
-----------------------------------------
1 honda yellow gasoline
2 bmw black diesel
Data in tables are to simplify the example.
Have tried to search over the internet for 2 days. No solution found. Need guidance what to search.
You need conditional aggregation either you can do PIVOT :
select id,
max(case when data-type = 'car' then data_answer end) as car,
max(case when data-type = 'color' then data_answer end) as color,
max(case when data-type = 'engine' then data_answer end) as engine
from table t
group by id;

Select users who answered all the questions

I have tables like these:
Table ANSWER
idAnswer idQuestion Status idGame
----------------------------------------
1 9 1 1
2 6 NULL 1
3 6 1 2
4 3 NULL 2
5 1 1 3
6 6 1 3
7 9 1 4
8 6 1 4
Table GAME:
idGame idUser
----------------
1 Greg
2 Greg
3 Jack
4 Frank
I want to get only those who answered the same question as the game n°4, like this:
Desired result:
idUser
-------
Greg
Frank
Status is used to check if the question has been answered. If it is NULL, the user didn't answer it.
Here, Frank had a very similar game (it's obvious, because it's the same, the n°4).
Greg didn't played a similar game, but answered in 2 others the same questions, so he appears in the result.
Jack did answered one of the question (n°6) but didn't answered the other one (n°9), so Jack doesn't appear.
So, the result includes only those who answered the same questions as another game, no matter if it's in a similar game or in multiple one.
Games are randomly generated. Sometimes, user can answer a question they already encounter or/and answered. (Like Greg, in game 1 and 2).
I tried a lot of queries. I can post them if you want.
Thanks for the answers!
You can try this:
SELECT g.idUser, COUNT(*) as NumberOfAnswers
FROM answer a
INNER JOIN game g ON a.idGame = g.idGame
WHERE a.idQuestion IN (9,6)
GROUP BY g.idUser
HAVING NumberOfAnswers = 2
I think this solution match your requirements if the users cannot answer the same question twice in the same game and if you doesn't mind to show users who has answered more questions.
EDIT:
For a more generic solution, you could adapt the query to something like this:
SELECT g.idUser, COUNT(*) as NumberOfAnswers
FROM answer a
INNER JOIN game g ON a.idGame = g.idGame
WHERE a.idCuestion IN (SELECT idQuestion FROM answer WHERE idGame=[DESIRED_GAME])
GROUP BY g.IdUser
HAVING NumberOfAnswers = (SELECT COUNT(*) FROM answer WHERE idGame = [DESIRED_GAME]

Query to retrieve main records in one table and its all sub records from another table

Let us suppose we have tables like questions and answers
Questions table
qno question
1 first question
2 second question
Answers table
ano qno answer
1 1 first answer for q1
2 1 second answer for q1
3 2 first answer for q2
4 1 third answer for q1
5 2 second answer for q2
Now I need a single query in mysql that can output each question and all of its answers
Expected output
qno ano question answer
--- --- ---------- -------------------
1 0 first question
1 1 first answer for q1
1 2 second answer for q1
1 4 third answer for q1
2 0 second question
2 3 first answer for q2
2 5 second answer for q2
Normally a JOIN doesn't output results in the format you show as your desired result. The JOIN operator in SQL matches rows from one table to rows from the other, so it will produce the question repeatedly, which is not what you want.
To get what you want, use UNION instead of JOIN, and position them into columns like you want:
(SELECT qno, 0, question, NULL AS `answer` FROM questions)
UNION
(SELECT qno, ano, NULL, answer FROM answers)
ORDER BY qno, ano
For what it's worth, I agree with the comments from Strawberry. I wouldn't actually do this in my application. I would fetch the data in a simpler format, and in my application code present the data in the layout desired.
My example above is only to show that manipulating the data into that layout in SQL makes the code less clear.

finding most 'popular' items in multiple tables in mySQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have multiple tables of items which are 'ordered' by their ratings/popularity.
I need to combine all the tables into one table with a top 10.
The top 10 will combine the number of times an entry (with wildcard as the names may be slightly different) appears in all the lists and it's position in the tables.
is this possible?
I've researched Joins but it seems quite a complicated procedure given there are two factors (nubmer of entries and position in the tables).
Apologies for being vague, I didn't think I was doing so. This is my first question on stackoverflow
table 1 table 2 table 3
--------------------
bob | bob | Ian
fred | james |john
kate | fred | bob
mary | brian | brian
the 'rankings' results of the three tables need to appear in a final table (called 'final' for example)
As you can see Bob would rank highly on 'final'.
But Ian appears only once, even though he is top of the list in table 3.
Fred appears in position 2 and position 3 so should he be higher or lower than Ian.
would I need an algorithm for the sorting or is there some trick in mySQl that will examine the rankings?
You can return them with a ranking, but you need to define how that rank applies.
For example if you just return the ranking from each table then Bob appears twice in the first position. If you add those 2 ranks together it gives 2. How do you compare that to Ian who is only ranked 1 once.
For this you are probably best building a ranking from the last row (or calculating it as total number of rows - ranking).
You can get a basic ranking from each table with the following:-
SELECT some_name, #rank_1:=#rank_1 + 1 AS ranking
FROM table_1
CROSS JOIN (SELECT #rank_1:=0) sub_1
UNION ALL
SELECT some_name, #rank_2:=#rank_2 + 1 AS ranking
FROM table_2
CROSS JOIN (SELECT #rank_2:=0) sub_2
UNION ALL
SELECT some_name, #rank_2:=#rank_2 + 1 AS ranking
FROM table_2
CROSS JOIN (SELECT #rank_2:=0) sub_3
but this will give you each record from each table and their ranking.
As it is though, you have nothing to say that Bob is the first record on table_1. While it may be the first record, as far as the order returned this is not a certainty.

Select distinct word count? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to put together a report that shows what search criteria a user used.
Right now it is basic and uses SELECT DISTINCT and then GROUP BY.
The problem is we would like to see this broken down by words too. The phrase criteria is useful but we would like to see:
Searches:
red apples are good
yellow bananas are bad
bad apples are not bananas
pears are not red
What we would like to see:
red 2
apples 2
are 4
good 1
yellow 1
bananas 2
bad 2
pears 1
not 2
I should note too that we have too me search terms to go through and write seperate %LIKE statements for them - and they change.
try this example
SELECT word,COUNT(*) as count
FROM
(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(searches,' '),' ',value.v),' ',-1) as word
FROM yourtable,(SELECT 1 as v UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5)value
)T
WHERE word != ''
GROUP BY word
sqlFiddle