This query returns all of the selected values from today's date to 90 days ago:
SELECT max(cases_visits.created_dt), users_profiles.account_num,
concat(cases.patient_first_initial,
cases.patient_middle_initial, cases.patient_last_initial) AS initials,
concat(users.first_name, ' ',users.last_name) as name
FROM cases
JOIN users_profiles
ON users_profiles.user_id=cases.doctor_id
JOIN cases_visits
ON cases.id=cases_visits.case_id
join users on users.id = cases.doctor_id
WHERE cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt BETWEEN curdate() - INTERVAL 90 DAY AND SYSDATE()
group by users.first_name
I'd like to find a query that will now select the exact same thing, but only if records DO not exist in the previous query. EXAMPLE: return records from > 90 days ago, that do not have records in the past 90 days.
I have tried to do this: (note, 2013-07-03 in the query was 90 days from the first time i ran)
SELECT cases_visits.created_dt, users_profiles.account_num,
concat(cases.patient_first_initial,
cases.patient_middle_initial, cases.patient_last_initial) AS initials,
concat(users.first_name, ' ',users.last_name) as name
FROM cases
JOIN users_profiles
ON users_profiles.user_id=cases.doctor_id
JOIN cases_visits
ON cases.id=cases_visits.case_id
join users on users.id = cases.doctor_id
WHERE cases_visits.created_dt < '2013-07-03'
group by users.first_name
This does not give me the proper data, I think because i need to somehow exclude records that exist from the past 90 days.
THIS IS WHAT IM TRYING TO DO: Select a records with aa value = to 'NP' for the past 90 days, then i need to select records where there is not a np value for greater than 90 days, but these records should be completely unique from the first query (ie the individual could have a case from within the 90 days, and one 180 days ago, i would not need his records.)
EDIT: I forgot to mention I have tried this query with an error near 'in':
SELECT cases_visits.created_dt, users_profiles.account_num,
concat(cases.patient_first_initial,
cases.patient_middle_initial, cases.patient_last_initial) AS initials,
concat(users.first_name, ' ',users.last_name) as name
FROM cases
JOIN users_profiles
ON users_profiles.user_id=cases.doctor_id
JOIN cases_visits
ON cases.id=cases_visits.case_id
join users on users.id = cases.doctor_id
WHERE cases_visits.created_dt < '2013-07-03'
and cases_visits.patient_visit_type = 'NP'
and not in (select created_dt from cases_visits where cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt BETWEEN curdate() - INTERVAL 90 DAY AND SYSDATE())
group by users.first_name
You could use a subquery:
select * from table where ID NOT IN (select id from table where a=1);
This would essentially select the records from the table that don't match the records the inner query did match.
SELECT * FROM table WHERE cases_visits.created_dt < '2013-07-03'
AND case_visist.SOME_UNIQUE_ID NOT IN
(SELECT case_visist.SOME_UNIQUE_ID FROM table WHERE cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt BETWEEN curdate() - INTERVAL 90 DAY AND SYSDATE() )
You can use NOT IN statement and enter a SELECT statement that select all the records that should be excluded. A some more info in this thread
This set should be visits for the same doctor, same cases but a different visit?
SELECT max(cases_visits.created_dt), users_profiles.account_num,
concat(cases.patient_first_initial,
cases.patient_middle_initial, cases.patient_last_initial) AS initials,
concat(users.first_name, ' ',users.last_name) as name
FROM cases
JOIN users_profiles
ON users_profiles.user_id=cases.doctor_id
JOIN cases_visits
ON cases.id=cases_visits.case_id
JOIN users
ON users.id = cases.doctor_id
WHERE cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt <= curdate() - INTERVAL 90 DAY
AND EXISTS(SELECT *
FROM case_visits AS inside
WHERE inside.case_id = cases.id
AND inside.patient_visit_type = 'NP'
AND inside.created_dt BETWEEN curdate() - INTERVAL 90 DAY AND SYSDATE())
GROUP BY account_num, initials, name
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.