3
I have a list of products purchased with date and price, I wanted to organize in a query the ten most purchased of each month, type:
mes | nome | total
janeiro | arroz | 100
janeiro | feijão | 87
... até a décima
fevereiro |feijão | 110
... e assim vai dentro de um intervalo como seis meses
table q have something like
id | id_cliente | nome | data_compra | preco_da_compra
32 | 12345678 | feijão | 2021-01-12 | 35
33 | 121434366 | arroz | 2021-01-11 | 25
34 | 223321213 | arroz | 2021-01-11 | 22
I have the following query
SELECT
DATE_FORMAT(data_compra, "%b") as mes,
nome,
(select count(distinct nome) from compras where compras.nome = nome) as total
WHERE
DATE_FORMAT(data_compra, "%b")= "Jan"
ORDER BY total DESC
LIMIT 10;
but this query takes only January the first ten, I wanted to get this same query month by month without having to manually change the date, in an interval like January to March.
some details like: which is sgdb (mysql, postgres)? and what means "most purchased", is a count of the quantity by name, or by the sum of the purchase price?
– William Bruno Rocha Moraes
In this case you would have to create a precedent, do one while a month as desired and go inserting this data into a table to query it later, or simply copy this select with the filter of the month and repeat it several times with Union all.
– Heitor Scalabrini
You could have informed the database structure and given some data that could help with the mass test. Here’s how to create a Minimum, Complete and Verifiable Example
– Sorack