0

此查询返回从今天日期到 90 天前的所有选定值:

 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

我想找到一个现在将选择完全相同的内容的查询,但前提是前一个查询中不存在记录。示例:返回 > 90 天前的记录,过去 90 天没有记录。

我试过这样做:(注意,查询中的 2013-07-03 是我第一次运行的 90 天)

    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

我认为这并没有给我正确的数据,因为我需要以某种方式排除过去 90 天存在的记录。

这就是我想要做的事情:选择过去 90 天内 aa 值 = 到 'NP' 的记录,然后我需要选择超过 90 天没有 np 值的记录,但这些记录应该完全与第一次查询不同(即个人可能在 90 天内有一个案例,而一个 180 天前的案例,我不需要他的记录。)

编辑:我忘了提到我已经尝试过这个查询,但在“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
4

3 回答 3

0

这组应该是同一个医生,同一个病例,但不同的访问?

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
于 2013-10-03T17:10:14.880 回答
0

您可以使用子查询:

select * from table where ID NOT IN (select id from table where a=1);

这实际上会从表中选择与内部查询匹配的记录不匹配的记录。

于 2013-10-03T16:48:54.547 回答
0
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() )

您可以使用 NOT IN 语句并输入一个 SELECT 语句来选择所有应排除的记录。此线程中的更多信息

于 2013-10-03T16:51:14.260 回答