简体   繁体   中英

How do I find a group of rows exclusively BETWEEN two dates in MySQL?

I have to find a group of results with a shared key that have been posted to the API of my local data store no earlier than the past 90 days, but no later than the past 7 days.

This would be a valid group for which I need to capture 1234:

account_id,company_id,posted_date
1234,A,2018-02-28
1234,B,2018-03-13
1234,C.2018-04-23
1234,D,2018-05-15

This would be an invalid group. If a single date falls outside of either the upper band or lower band of the query, the account ID should be excluded from the final results:

account_id,company_id,posted_date
5678,Z,2018-02-01
5678,Y,2018-03-13
5678,X.2018-04-23
5678,W,2018-05-21

This is the first draft of a query using subqueries:

SELECT DISTINCT account_id, company_id FROM local_data_store.result_api
WHERE account_id NOT IN (
    SELECT account_id FROM local_data_store.result_api
    GROUP BY account_id
    HAVING posted_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
)
AND account_did IN (
    SELECT account_did FROM local_data_store.result_api
    GROUP BY account_did
    HAVING posted_date > DATE_SUB(NOW(), INTERVAL 90 DAY)
)
GROUP BY account_id, company_id
LIMIT 100000;

This is the query I'm working on now without subqueries (I tried joins but they really didn't work):

SELECT DISTINCT account_id, company_id, 
COUNT(ra1.posted_date > DATE_SUB(NOW(), INTERVAL 90 DAY)) AS day90, 
COUNT(ra1.posted_date > DATE_SUB(NOW(), INTERVAL 7 DAY)) as day7
FROM local_data_store.result_api ra1
GROUP BY posted_date, account_id;

But it runs for so long that the database connection times out. This is only on a database table of 375,000 rows.

Here's the solution I came up with. Hope it helps someone else.

SELECT DISTINCT account_did, cb_company_id, COUNT(did) as `# of jobs`
FROM cb_local_data_store.job_search_result_api 
WHERE account_did NOT IN (
SELECT account_did FROM cb_local_data_store.job_search_result_api
WHERE posted_date < DATE_SUB(NOW(), INTERVAL 60 DAY)
OR posted_date > DATE_SUB(NOW(), INTERVAL 4 DAY) )
GROUP BY account_did, cb_company_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM