簡體   English   中英

使用嵌套查詢創建視圖

[英]Creating a view with nested queries

 Use Surveydb;  
 create view VW_Service
    As 
    (select A.id as 'Encounter ID'
           ,A.startDateTime as 'Enconter StartDateTime'
           ,A.endDateTime as 'Encounter EndDateTime'
           ,B.id as 'Service ID'
           ,B.startDateTime as 'Service StartDateTime'
           ,B.endDateTime as 'Service EndDateTime'
           ,C.label as 'Services Name Code Label'
           ,C.symbol as 'Service Name Code Symbol'
           ,C.system as 'Service Name Code System'
     from Code C,
          Encounter A,
          Service B
     where 
     a.id = b.encounterId
     and c.id = b.nameCodeId) 
     JOIN
     (select a.label as 'Service Status Code Label'
           ,a.symbol as 'Service Status Code Symbol'
           ,b.system as 'Services Status Code System'
      from
      Code a, Code b
      Where
      a.id = b.id ) 

我正在嘗試創建一個包含三個表的視圖,並且還需要自連接其中一個表。 上面的腳本是視圖的單獨查詢。 第一個查詢由所有三個表組成,第二個子查詢是代碼表中的自聯接查詢。 我正在嘗試加入兩個查詢。 有什么想法嗎?

2個子查詢的聯接非常簡單:您只需獲得2個子查詢,然后像往常一樣將它們聯接。 像這樣:

select *
from (select id from table a) as a
join (select id from table b) as b
  on a.id = b.id

您只需要知道如何加入這些子查詢即可。 就您而言,我認為這是表代碼中的某些內容,例如id字段。

但是您的“自我加入”是有線的,您根本不需要它,這將給您相同的結果:

join (select a.label as 'Service Status Code Label'
       ,a.symbol as 'Service Status Code Symbol'
       ,a.system as 'Services Status Code System'
  from Code a
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM