I'm attempting to create a SQL statement that does the following but obviously failing at it.
Selects all the information from a view and filters it based on the following criteria:
- If the given field "customer_number" exists at any point in the table
- Then check for any duplicates within the last 30 days
- Then output all distinct customer_numbers grouped by customer_number
- Have a count so that I can see how many times an item was repeated.
So using today as our current date say I had the following data entries in my table:
customer_number, date
111111, 2013-01-01
111111, 2013-05-05
222222, 2013-01-07
222222, 2013-03-19
333333, 2013-05-15
333333, 2013-05-30
I would want my SELECT statement to return the following:
customer_number, date, count
111111, 2013-01-01, 2
333333, 2013-05-15, 2
And customer 222222 would not be displayed because he does not appear in the last 30 days.
Here's about as far as I've gotten trying to figure this out. It only returns data for the current month.
SELECT *, COUNT(customer_number)
FROM red_flags
WHERE dealer_id = '75' AND date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
GROUP BY customer_number
HAVING COUNT(customer_number) > 1
I've been melting my brain trying to figure out how or if this is even possible to do in a query any help would be greatly appreciated!