Jess Tremblay
Jess Tremblay

Reputation: 45

MYSQL SUM The count value

I'm trying to SUM the Count Value then divide it to 2 But sadly my query is wrong

**This is my actual Database**
id|      date_opened    |
1 | 2021-03-05 11:27:31 |
2 | 2021-03-05 11:27:31 |
3 | 2021-03-05 00:03:45 |
4 | 2021-03-05 00:04:02 |
5 | 2021-03-04 00:00:05 |
6 | 2021-03-03 00:00:00 |
7 | 2021-03-03 08:46:33 |

And what I did is I count the same value (date)

My query to Count Same Value (Working)

SELECT COUNT(DATE(date_opened)) as 'Total User for this Date'
FROM daily_report
WHERE DATE(date_opened)>=DATE_SUB(CURDATE(), INTERVAL 3 DAY )
GROUP BY DATE(date_opened) DESC

This is the result of the query above

   |Total User for this day|
   |          4            |
   |          1            |
   |          2            |

and then I add the Sum Function and divide it to two

SELECT SUM('Total User for this Date') FROM (
SELECT COUNT(DATE(date_opened)) as 'Total User for this Date'
FROM daily_report
WHERE DATE(date_opened)>=DATE_SUB(CURDATE(), INTERVAL 3 DAY )
GROUP BY DATE(date_opened) DESC)/2

And i got this error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 5

    This is what I want to hapen
       |Total User for this day|
       |          3.5           |
 

Is it possible to Sum the Count data and divide it to 2 in Mysql? if it is, Can someone teach me to do it or just point me to right direction

Upvotes: 0

Views: 189

Answers (1)

Akina
Akina

Reputation: 42622

SELECT COUNT(*)/2 as `Total User for this Day`
FROM daily_report
WHERE date_opened >= CURRENT_DATE - INTERVAL 3 DAY

Upvotes: 1

Related Questions