Sepertinya Anda perlu menambahkan fungsi agregat:
SELECT CD_NO, purchase_date, Count(purchase_date) totalSales
FROM Artist
GROUP BY purchase_date
ORDER BY CD_NO
MySQL memungkinkan Anda untuk membatasi jumlah bidang yang Anda GROUP BY
jadi ini di atas hanya pengelompokan pada purchase_date
.
Di RDBMS lain, Anda bisa menggunakan subquery:
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
Sunting:Berdasarkan pembaruan Anda, Anda seharusnya dapat menggunakan sesuatu yang mirip dengan ini:
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);
Lihat SQL Fiddle dengan Demo
Hasil:
| CD_NO | MOSTSALES |
----------------------
| 1 | 2011-12-30 |
| 2 | 2012-03-22 |
| 3 | 2012-04-22 |
| 3 | 2012-04-24 |