Het klinkt alsof je een verzamelfunctie moet toevoegen:
SELECT CD_NO, purchase_date, Count(purchase_date) totalSales
FROM Artist
GROUP BY purchase_date
ORDER BY CD_NO
Met MySQL kunt u het aantal velden beperken dat u GROUP BY
dus dit hierboven is alleen gegroepeerd op de purchase_date
.
In andere RDBMS kunt u een subquery gebruiken:
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
Bewerken:op basis van je updates zou je zoiets als dit moeten kunnen gebruiken:
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);
Resultaat:
| CD_NO | MOSTSALES |
----------------------
| 1 | 2011-12-30 |
| 2 | 2012-03-22 |
| 3 | 2012-04-22 |
| 3 | 2012-04-24 |