Låter som att du behöver lägga till en aggregatfunktion:
SELECT CD_NO, purchase_date, Count(purchase_date) totalSales
FROM Artist
GROUP BY purchase_date
ORDER BY CD_NO
MySQL låter dig begränsa antalet fält som du GROUP BY
så detta ovan är bara gruppering på purchase_date
.
I andra RDBMS kan du använda en underfråga:
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
Edit:Baserat på dina uppdateringar bör du kunna använda något liknande detta:
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);
Resultat:
| CD_NO | MOSTSALES |
----------------------
| 1 | 2011-12-30 |
| 2 | 2012-03-22 |
| 3 | 2012-04-22 |
| 3 | 2012-04-24 |