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

Consulta no completa - MYSQL

Parece que necesitas agregar una función agregada:

SELECT CD_NO, purchase_date, Count(purchase_date) totalSales
FROM Artist
GROUP BY purchase_date
ORDER BY CD_NO

MySQL le permite limitar la cantidad de campos que GROUP BY así que esto de arriba solo se agrupa en el purchase_date .

En otros RDBMS, podría usar una subconsulta:

SELECT a1.CD_NO, a1.purchase_date, a2.TotalSalesByDate
FROM Artist a1
INNER JOIN
(
    SELECT count(purchase_date) TotalSalesByDate, purchase_date
    FROM Artist
    GROUP BY purchase_date
) a2
   on a1.purchase_date= a2.purchase_date
ORDER BY a1.CD_NO

Editar:según sus actualizaciones, debería poder usar algo similar a esto:

SELECT a1.CD_NO, date_format(a1.purchase_date, '%Y-%m-%d') MostSales
FROM Artist a1
INNER JOIN
(
    SELECT count(purchase_date) TotalSalesByDate, purchase_date
    FROM Artist
    GROUP BY purchase_date
) a2
   on a1.purchase_date= a2.purchase_date
group by a1.CD_NO, a1.purchase_date
having max(TotalSalesByDate) = (select max(Total) totalsales
                                 from
                                 (
                                    SELECT cd_no, purchase_date, Count(purchase_date) Total
                                    FROM Artist a1
                                    GROUP BY cd_no, purchase_date
                                 ) src
                                 where a1.cd_no = src.cd_no
                                 GROUP BY cd_no);

Consulte SQL Fiddle con demostración

Resultado:

| CD_NO |  MOSTSALES |
----------------------
|     1 | 2011-12-30 |
|     2 | 2012-03-22 |
|     3 | 2012-04-22 |
|     3 | 2012-04-24 |