sql >> Base de Datos >  >> RDS >> Mysql

mysql agrupando por semana

Si almacena the_date como entero, primero debe convertirlo a fecha y hora usando FROM_UNIXTIME función:

 SELECT SUM(`amount_sale`) as total 
FROM `sales` 
WHERE `payment_type` = 'Account' 
GROUP BY WEEK(FROM_UNIXTIME(`the_date`))  

ACTUALIZAR :
Además, es posible que desee generar el número de semana,

SELECT CONCAT('Week ', WEEK(FROM_UNIXTIME(`the_date`))) as week_number,
SUM(`amount_sale`) as total 
FROM `sales` 
WHERE `payment_type` = 'Account' 
GROUP BY WEEK(FROM_UNIXTIME(`the_date`))