performing mathematical operations on the query

I’m using a query to return the total number of records x number of days that were registered… I would like to have the average too, how can I get this result?

SELECT COUNT(DISTINCT CAST(DataCriacao AS DATE))  AS dias, userid, COUNT(*), SUM(count(*) / dias) FROM ticket WHERE MONTH(DataCriacao) = month(CURDATE()) AND YEAR(DataCriacao) = YEAR(CURDATE()) AND motivoRegistro LIKE '%Ap%' GROUP BY userid ORDER BY COUNT(*) DESC

expected

+--------+------+----------+-----+
| userid | dias | count(*) | avg |
+--------+------+----------+-----+
| root   |   10 |       50 |   5 |
+--------+------+----------+-----+

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Do you just want division?

SELECT COUNT(DISTINCT CAST(DataCriacao AS DATE)) AS dias, userid, COUNT(*),
       count(*) / COUNT(DISTINCT CAST(DataCriacao AS DATE))
FROM ticket
WHERE MONTH(DataCriacao) = month(CURDATE()) AND
      YEAR(DataCriacao) = YEAR(CURDATE()) AND
      motivoRegistro LIKE '%Ap%'
GROUP BY userid
ORDER BY COUNT(*) DESC


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x