My query so far:
SELECT CD_no, purchasedate, COUNT(*) mostsales
FROM Sales A
GROUP BY CD_no, purchasedate
HAVING COUNT(*) =
(SELECT MAX(mostsales)
FROM
(SELECT CD_no, purchasedate, COUNT(*) AS mostsales
FROM Sales
GROUP BY CD_no, purchasedate) B
WHERE CD_no = A.CD_no)
My query so far generates from an input of CD_no's and purchase's, finds date had the most sales for that specific CD. However this only returns one value for each CD number. However there may be a possibility that a CD_no may have the exact same maximal purchases on two different dates, and therefore should display both dates against the CD number.
Current output:
CD_NO mostsales
1 2011-12-30
2 2012-03-22
3 2012-04-24
Desired output:
CD_NO mostsales
1 2011-12-30
2 2012-03-22
3 2012-04-24
3 2012-04-22
From the following input:
CD_NO purchase_date
1 2011-12-30
1 2011-12-30
1 2011-12-29
1 2011-12-28
2 2012-03-22
2 2012-03-22
2 2012-03-21
3 2012-04-24
3 2012-04-24
3 2012-04-22
3 2012-04-22
3 2012-04-21