MySQL select distinct dateTime. Cant figure this query out - mysql

My users answer two different questions, they are added to my answers_table as one row each. I'm trying to count how many users have answered my questions which seems simple at first. Count(*) and divide by 2. YES! But: the users can add new questions as they go, so suddenly there might be three questions, all answers/3 becomes an incorrect number of users if there was two questions for a while and then three, for example!
Can anyone figure out a query for doing this? So far I've got
SELECT COUNT(DISTINCT date) as totNum FROM login_answers
But it doesn't take the time in consideration. Meaning i just get the amount of answers in total for that day, not knowing how many questions was available that day.
Here's my table:
qid is the QuestionID so i guess thats somewhere to start?
id qid answer date deviceID
1502 2 2 2012-10-19 08:42:41 7
1503 1 3 2012-10-19 08:51:53 7
1504 2 2 2012-10-19 08:51:53 7
1505 1 4 2012-10-19 09:05:23 7
1506 2 2 2012-10-19 09:05:23 7
1507 1 4 2012-10-19 09:40:59 7

My proposed solution would be to store a session Id or some auto-generated key against the answers to uniquely identify a "session" of 2, 3 or more answers from a single person.
The query would simply be
SELECT COUNT(DISTINCT session_id) totNum
FROM login_answers
With your current schema with no identifiable key, it's quite hard to answer your question correctly if at all possible.

Related

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]

SQL find ID by Sum of rates

Hey cant find the right answer for my question.
If I have a table :
DB :rate
1 id int(11)
2 type varchar(256)
3 candidate_id int(11)
4 data text(rating data is inside here 1-10)
I want all Candidate_ids as result where the average rate is a search string for example 3. How can I get this happen? I tried it but cant get it on my own.
Every candidate has more than 5 Records in the DB:rate
id type candidate_id data
1 vote 5 10
2 vote 5 4
3 vote 4 4
4 vote 4 3
5 vote 5 8
Assuming I understand the question, I think you are looking for something like this:
SELECT candidate_id
FROM TableName
GROUP BY candidate_id
HAVING AVG(data) = 3
Your question is a bit difficult to follow. You want all Candidates with an average rate of 3, but your table doesn't feature a column "rate" - did you mean "data"? However, IF I got your question right, something like ( ! ) the following should solve the problem:
SELECT Candidate_ID
FROM [RateTable]
GROUP BY Candidate_ID
HAVING Avg(rate) = 3

Using two columns to obtain count on another in SQL

I have a simple question that I wasn't really sure how to search for (or title!). I apologize if this has been asked a million times. For the following table, how do I generate a report that will detail the number of companies that a person has worked for and how many people have also worked for that same number? So, for example, this table should return:
people, companiesperperson
1, 1
2, 2
1, 3
for the following table called personalinfo:
id_number first last company
1 John Doe Intel
2 John Doe Microsoft
3 Phil Jenkins Amgen
4 Phil Jenkins Bayer
5 Phil Jenkins Sanofi
6 Josh Edwards Walgreens
7 Amy Dill URS
8 Amy Dill ARCADIS
Let me know if this is still confusing and if I can further clarify what I am looking to do.
Thanks!
This is a rough estimate of the query but
SELECT count as companiesperperson, COUNT(first, last) as people FROM
(SELECT COUNT(company) as count, first, last FROM personalinfo GROUP BY (first, last)) as a
GROUP BY count
To explain the query first in the subquery we are asking for the names and count of companies after splitting up all the rows by names
Then in the outer query we split up all the rows by their count and ask how many unique names can be found in each group.
There may be a few syntax errors I've left straggling but the group by feature is really what's essential to understanding how to solve this question.

MySQL, Select, Merge tables

I have a little problem that I can´t solve. It´s really simple, but I just can´t figure it out and have search some time but not found any good answers.
I have two tables:
Transaction
t_nr (Primary) a_nr quantity
1 1 10
2 2 10
Customer
c_nr (PRIMARY) name city
1 Mario Tokyo
2 Luigi Beijing
And want to insert values from the two above into another table with one query looking
Account
a_nr (primary) c_nr
Problem is that when just making a regular select-from-statement it returns:
a_nr c_nr
1 1
1 2
2 1
2 2
i.e. not just merges them together in the account table.
a_nr c_nr
1 1
2 2
How do I do this?
Does a_nr correlate to c_nr (are they equal)?
If so,
insert into account (a_nr,c_nr)
SELECT transaction.a_nr, customer.c_nr from transaction, customer
WHERE transaction.a_nr = customer.c_nr
Although this seems completely pointless to only insert two values that are the same.
What is the desired output of Account?

MS Access 2007 Rows to columns in recordset

I have a table which is like a questionnaire type ..
My original table contains 450 columns and 212 rows.
Slno is the person's id who answer the questionaire .
SlNo Q1a Q1b Q2a Q2b Q2c Q2d Q2e Q2f .... Q37c <450 columns>
1 1
2 1 1
3 1
4 1 1
5 1
I have to do analysis for this data , eg Number of persons who is male (Q1a) and who owns a boat (Q2b) i.e ( select * from Questionnaire where Q1a=1 and Q2b=1 ).. etc .. many more combinations are there ..
I have designed in MS access all the design worked perfectly except for a major problem ( Number of table columns is restricted to 255 ).
To be able to enter this into access table i have inserted in as 450 rows and 212 columns (now am able to enter this into access db). Now while fetching the records i want the record set to transpose the results into the form that i wanted so that i do not have to change my algorithm or logic .... How to achieve this with the minimum changes ? This is my first time working with Access Database
You might be able to use a crosstab query to generate what you are expecting. You could also build a transpose function.
Either way, I think you'll stil run into the 255 column limit and MS Access is using temporary table, etc.
However, I think you'll have far less work and better results if you change the structure of your table.
I assume that this like a fill-in-the-bubble questionnaire, and it's mostly multiple choice. In which case instead of recording the result, I would record the answer for the question
SlNo Q1 Q2
1 B
2 B
3 A
4 A C
5 A
Then you have far fewer columns to work with. And you query for where Q1='A' instead of Q1a=1.
The alternative is break the table up into sections (personal, career, etc.) and then do a join, and only show the column you need (so as not to exceed that 255 column limit).
An way to do this that handles more questions is have a table for the person, a table for the question, and a table for the response
Person
SlNo PostalCode
1 90210
2 H0H 0H0
3
Questions
QID, QTitle, QDesc
1 Q1a Gender Male
2 Q1b Gender Female
3 Q2a Boat
4 Q2b Car
Answers
SlNo QID Result
1 2 True
1 3 True
1 4 True
2 1 True
2 3 False
2 4 True
You can then find the question takers by selecting Persons from a list of Answers
select * from Person
where SlNo in (
select SlNo from Answers, Questions
where
questions.qid = answers=qid
and
qtitle = 'Q1a'
and
answers.result='True')
and SlNo in (
select SlNo from Answers, Questions
where
questions.qid = answers=qid
and
qtitle = 'Q2a'
and
answers.result='True')
I finally got the solutions
I created two table one having 225 columns and the other having 225 column
(total 450 columns)
I created a SQL statement
select count(*) from T1,T2 WHERE T1.SlNo=T2.SlNo
and added the conditions what i want
It is coming correct after this ..
The database was entered wrongly by the other staff in the beginning but just to throw away one week of work was not good , so had to stick to this design ... and the deadly is next week .. now it's working :) :)