I have table with over 35 million email records.
I need to get all of the records between a date range and also exist at least 3 times in the table.
1 email may have more than one property but will never have the same property twice.
The table looks like this:
PROPERTY | LAST_SENT_DATE | |
---|---|---|
email1 | prop1 | 2024-06-25 |
email2 | prop1 | 2024-06-16 |
email3 | prop2 | 2024-06-02 |
email4 | prop3 | 2024-06-25 |
email1 | prop2 | 2024-06-26 |
email1 | prop3 | 2024-06-10 |
So I want to write a query that will return the emails in June but exist more than 3 times.
SELECT * FROM `myTable`
WHERE
(
(`last_sent_date` BETWEEN '2024-06-01' AND '2024-06-30')
AND
(//...SOME PROPERTY COUNT HERE)
)
The results should look like this:
PROPERTY | LAST_SENT_DATE | |
---|---|---|
email1 | prop1 | 2024-06-25 |
email1 | prop2 | 2024-06-26 |
email1 | prop3 | 2024-06-10 |
I’m guessing it needs to do some kind of COUNT on PROPERTY, but I’m not sure where or how to add it.
2
Answers
fiddle
Well, it’s pretty much straightforward, you can use an
EXIST
clause. this is the more efficient way because it allows the database to stop searching as soon it finds a matching row:Here is the code:
This query uses an
EXIST
clause to check if there is at least one row in the subquery is matches theemail
column.Just advice: consider creating an index on the
email
andlast_sent_date
columns to improve the performance.