I have a table names lcMovimientos and what I need is a query where I get as a result the sum of the quantity of cantidadMovimientos but I want each row to be by the day of the week and only to sum the days of the current week, so after researching I found the best way to do this was creating a new table with the days of the week, so I now have a table called diasSemana
SELECT * FROM diasSemana
+-----------+
| diaSemana |
+-----------+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+-----------+
and a table called lcMovimientos
mysql> DESCRIBE lcMovimientos;
+-----------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+----------------+
| idMovimiento | int(11) | NO | PRI | NULL | auto_increment |
| idUsuario | int(11) | YES | | NULL | |
| tipoMovimiento | tinyint(4) | YES | | NULL | |
| cantidadMovimiento | decimal(20,2) | YES | | NULL | |
| idCategoria | int(11) | YES | | NULL | |
| fechaMovimiento | date | YES | | NULL | |
| idCuenta | int(11) | YES | | NULL | |
| descripcionMovimiento | varchar(255) | YES | | NULL | |
| etiquetasMovimiento | varchar(255) | YES | | NULL | |
+-----------------------+---------------+------+-----+---------+----------------+
I can make a query where I do get the sum of the cantidadMovimiento but when I add the where clause so I only get results from the current week, I no longer get the rows by day fo the week, so here is my query:
mysql> SELECT SUM( cantidadMovimiento ) , diaSemana, fechaMovimiento -> FROM diasSemana
-> LEFT JOIN lcMovimientos ON diaSemana = WEEKDAY( fechaMovimiento )
-> GROUP BY diaSemana;
+---------------------------+-----------+-----------------+
| SUM( cantidadMovimiento ) | diaSemana | fechaMovimiento |
+---------------------------+-----------+-----------------+
| 280.00 | 0 | 2012-02-20 |
| 800.00 | 1 | 2012-02-21 |
| 7000.00 | 2 | 2012-02-29 |
| NULL | 3 | NULL |
| NULL | 4 | NULL |
| -3300.78 | 5 | 2012-02-18 |
| 600.00 | 6 | 2012-02-26 |
+---------------------------+-----------+-----------------+
and when I use the WHERE clause:
mysql> SELECT SUM( cantidadMovimiento ) , diaSemana, fechaMovimiento
-> FROM diasSemana
-> LEFT JOIN lcMovimientos ON diaSemana = WEEKDAY( fechaMovimiento )
-> WHERE WEEK( fechaMovimiento, 1 ) = WEEK( CURRENT_DATE, 1 )
-> GROUP BY diaSemana;
+---------------------------+-----------+-----------------+
| SUM( cantidadMovimiento ) | diaSemana | fechaMovimiento |
+---------------------------+-----------+-----------------+
| 265.00 | 0 | 2012-02-20 |
| 800.00 | 1 | 2012-02-21 |
| 600.00 | 6 | 2012-02-26 |
+---------------------------+-----------+-----------------+
So my question is how can i make a query where I will get the results by the day of the week using the where to only get dates of the current week??? thank you so much in advance!
try this