Calculate difference between timestamp field with specific timestamp

Asked

Viewed 840 times

0

I’m trying to develop a ranking chart for a kind of questionnaire.

Each day a question is asked at 4pm, which can be answered until 5:59:59 the next day. The table has to show the position of the participants taking into account the number of right answers and time.

That means you answer right and faster you get first. The number of questions answered also counts, if Arque does not answer a day is penalized.

My chart will be like:

+-------+---------+---------------------+
|userid | acertou |      timestamp      |
+-------+---------+---------------------+
|   2   |    1    | 2018-02-07 16:00:01 |
|   1   |    1    | 2018-02-07 16:02:00 |
|   3   |    1    | 2018-02-07 17:00:00 |
|   1   |    0    | 2018-02-08 16:00:02 |
|   3   |    1    | 2018-02-08 16:00:05 |
|   2   |    0    | 2018-02-08 16:01:00 |
+-------+---------+---------------------+

I started with this query:

SELECT `userid`, `acertou`, `timestamp`, 
count(acertou) as cont 
FROM `resultados` 
WHERE acertou = 1
GROUP BY `userid `
ORDER BY cont DESC, timestamp DESC

But I realize that this is not what I want because the ranking has to be cumulative but taking into account the various days.

Does anyone have any idea how I can do this?

  • makes a SUM on the score and a average calculation by the difference of time Stamp.

  • Calculates the person’s 'note' in another column and makes a SUM() per user. I suggest to make a function that receives the hit and the date-time and calculate the note, so it is easier if you need to change the rule or time.

  • but I have no score, just got right or wrong question

  • You want to know who hit the hardest and in less time is it ?

  • right, considering it’s one question a day.

1 answer

1

As in chat, your problem is that you don’t have a reference to the start date of the questions, so you can’t calculate the difference between the dates.

Having this table with the dates and questions, the select would be basically this way:

-- soma acertos, e verifica a diferença em horas, minutos, segundos
SELECT userid, SUM(acertou),
-- Diferença em horas
SUM(TIMESTAMPDIFF(HOUR,timestamp,CAST(CONCAT_WS(' ',date(timestamp), '17:59:59') as DATETIME))) horas,
-- Diferença em minutos
SUM(TIMESTAMPDIFF(MINUTE,timestamp,CAST(CONCAT_WS(' ',date(timestamp), '17:59:59') as DATETIME))) minutos,
-- Diferença em segundos
SUM(TIMESTAMPDIFF(SECOND,timestamp,CAST(CONCAT_WS(' ',date(timestamp), '17:59:59') as DATETIME))) segundos
FROM resultados
-- agrupa por usuário
GROUP BY userid
-- OPICIONAL, verifica se o usuário respondeu todas as perguntas
HAVING SUM(acert) = (SELECT COUNT(*) FROM (SELECT DISTINCT(perg_n) FROM resultados) qtd_perguntas)
-- ordena pelo que tem mais acertos, depois pelos tempos ( quanto mais tempo sobrou, menos tempo usou.
ORDER BY acertos DESC, horas DESC, minutos DESC, segundos DESC;
  • only that the result is not only by the right answers, but by those who took less to answer. has q falzer an average of the seconds it took him to respond from each day.

  • So it’s "right answers through time"...

  • I was testing but gave the following error: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ') tempo FROM resultados GROUP BY userid ORDER BY hit DESC, tempo' at line 2

  • Lacked a ). Corrected !

  • yet from the error, I don’t understand You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ')) time FROM results GROUP BY userid ORDER BY hit DESC, time' at line 2

  • Hit try again.

  • First of all thanks for the help, no more error however the results are not what I’m looking for. Note that there is a question every day, it is not only the speed of the answer but who answered every day right.

  • Then I still don’t understand how you want me to display the result. You need to explain better, for example: I need to filter out all the ones that got it right EVERY DAY, and see who answered faster. Only in this example already changes the WHERE = 1, because it will only filter the right answers but does not know how many are in total. Among other factors. You need to explain in detail so we can help you.

  • @RBZ edited the question explaining better

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.