简体   繁体   English

Oracle SQL Query 连接多个表

[英]Oracle SQL Query to join multiple tables

I am trying to join multiple tables and the below query works well.我正在尝试加入多个表,下面的查询运行良好。

select 
    e.ENCNTR_ID e_id
    , p.PERSON_ID p_id
    , e.REG_DT_TM admitted


from   
    episode e 
    , patient  p 
where (e.reg_dt_tm > '01/JAN/2018' 
       and e.reg_dt_tm < '02/JAN/2018' 
       and e.active_ind = 1
       and e.encntr_type_cd = 123.00 
       and e.ACTIVE_STATUS_CD = 1234 
       and e.person_id = p.person_id)

But when I change it and add more tables it gives me the error但是当我更改它并添加更多表时,它给了我错误

"SQL Command not properly ended" “SQL 命令未正确结束”

I need to add conditions on the first table(episode) as otherwise the query runs very slow.我需要在第一个表(剧集)上添加条件,否则查询运行速度非常慢。

select 
    e.ENCNTR_ID e_id
    , p.PERSON_ID p_id
    , e.REG_DT_TM admitted
    , ce.EVENT_ID event_id

from   
    ENCOUNTER e 
    , person  p 
where (e.reg_dt_tm > '01/JAN/2018' 
       and e.reg_dt_tm < '02/JAN/2018' 
       and e.active_ind = 1
       and e.encntr_type_cd = 7113.00 
       and e.ACTIVE_STATUS_CD = 22223 
       and e.person_id = p.person_id)
left join CLINICAL_EVENT ce on ce.ENCNTR_ID = e.ENCNTR_ID      
                            and ce.EVENT_CD in (1235764 
                                                ,22161234 
                                                )                  
                            and ce.valid_until_dt_tm > sysdate
left join CE_BLOB cb on ce.EVENT_ID = cb.EVENT_ID
                    and cb.valid_until_dt_tm > sysdate

order by e.REG_DT_TM, ce.PERFORMED_DT_TM, ce.CLINICAL_EVENT_ID

The query should look like this:查询应如下所示:

select e.ENCNTR_ID as e_id, p.PERSON_ID as p_id, e.REG_DT_TM as admitted, ce.EVENT_ID as event_id
from ENCOUNTER e join
     person p 
     on e.person_id = p.person_id left join
     CLINICAL_EVENT ce
     on ce.ENCNTR_ID = e.ENCNTR_ID and   
        ce.EVENT_CD in (1235764, 22161234) and                
        ce.valid_until_dt_tm > sysdate left join
     CE_BLOB cb
     on ce.EVENT_ID = cb.EVENT_ID and
        cb.valid_until_dt_tm > sysdate
where e.reg_dt_tm > date '2018-01-01' and
      e.reg_dt_tm < date '2018-01-02' and
      e.active_ind = 1 and
      e.encntr_type_cd = 7113.00 and
      e.ACTIVE_STATUS_CD = 22223 
order by e.REG_DT_TM, ce.PERFORMED_DT_TM, ce.CLINICAL_EVENT_ID;

Notes:笔记:

  • Never use commas in the FROM clause.切勿FROM子句中使用逗号。 Always use proper, explicit, standard JOIN syntax.始终使用正确、明确、标准的JOIN语法。
  • The WHERE clause goes after the FROM clause. WHERE子句在FROM子句之后。
  • JOIN is an operator in the FROM clause, so all JOIN s need to be before the WHERE . JOINFROM子句中的运算符,因此所有JOIN需要在WHERE之前。
  • Use the keyword DATE for date constants.对日期常量使用关键字DATE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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