2016-03-09 69 views
0

我在數年內沒有在大型複雜規模中使用過sql server,並且找不到適當的sintax相交類型查詢來幫助這兩個數據集,並且不會創建重複名稱。一些患者將有一個訂單和一個臨牀事件進入,一些患者只會有一個臨牀事件。沒有重複的相交查詢

數據集1

SELECT 
    distinct 
    ea.alias as FIN, 
    per.NAME_Last + ', ' + per.NAME_FIRST + ' ' + Isnull(per.NAME_MIDDLE, '')  as PatientName, 
oa.action_dt_tm as CirOrder, 
od.ORIG_ORDER_DT_TM as DischOrder, 
e.disch_dt_tm as ActualDisch, 
prs.NAME_FULL_FORMATTED as OrderedBy, 
from pathway py 
join encounter e on e.CERNER_ENCOUNTER_ID = py.encntr_id 
join encntr_alias ea on ea.CERNER_ENCNTR_ID = e.CERNER_ENCOUNTER_ID and ea.ENCNTR_ALIAS_TYPE_WCD = 1049 
join person per on per.CERNER_PERSON_ID = e.cerner_PERSON_ID 
join orders o on o.CERNER_ENCNTR_ID= e.CERNER_ENCOUNTER_ID and o.CATALOG_wCD = '82111' -- communication order 
      and o.pathway_catalog_id = '43809296' ---Circumcision Order 
join order_action oa on oa.[CERNER_ORDER_ID] = o.CERNER_ORDER_ID and oa.ACTION_TYPE_WCD = '2494'--ordered 
join orders od on od.CERNER_ENCNTR_ID= e.CERNER_ENCOUNTER_ID and od.CATALOG_WCD = '203520' --- Discharge Patient 
join prsnl prs on prs.CERNER_PERSON_ID = oa.order_provider_id 
where py.pathway_catalog_id = '43809296' and  ---Circumcision Order 
oa.action_dt_tm > '2016-01-01 00:00:00' 
and oa.ACTION_DT_TM < '2016-01-19 23:59:59' 
--use the report prompts as parameters for the action_dt_tm 

數據集2

SELECT 
distinct e.[CERNER_ENCOUNTER_ID], 
ea.alias as FIN, 
per.NAME_Last + ', ' + per.NAME_FIRST + ' ' + Isnull(per.NAME_MIDDLE, '') as PatientName, 
ce.EVENT_END_DT_TM as CircTime, 
od.ORIG_ORDER_DT_TM as DischOrder, 
e.disch_dt_tm as ActualDisch, 
'' OrderedBy, -- should be blank for this set 
cv.DISPLAY 
from encounter e 
join clinical_event ce on e.CERNER_ENCOUNTER_ID = ce.CERNER_ENCNTR_ID 
join encntr_alias ea on ea.CERNER_ENCNTR_ID = e.CERNER_ENCOUNTER_ID and ea.ENCNTR_ALIAS_TYPE_WCD = 1049 
join person per on per.CERNER_PERSON_ID = e.cerner_PERSON_ID 
join orders od on od.CERNER_ENCNTR_ID= e.CERNER_ENCOUNTER_ID and od.CATALOG_WCD = '203520' --- Discharge Patient 
left outer join ENCNTR_LOC_HIST elh on elh.CERNER_ENCNTR_ID = e.CERNER_ENCOUNTER_ID 
left outer join CODE_VALUE cv on cv.CODE_VALUE_WK = elh.LOC_NURSE_UNIT_WCD 
where ce.event_wcd = '201148'  ---Newborn Circumcision 
and ce.[RESULT_VAL] = 'Newborn Circumcision' 
and ce.EVENT_END_DT_TM > '2016-01-01 00:00:00' 
and ce.event_end_dt_tm < '2016-01-19 23:59:59’ 
and ce.RESULT_STATUS_WCD = '25' 
and elh.ACTIVE_STATUS_DT_TM < ce.event_end_dt_tm -- Circ time between the location's active time and end time. 
and elh.END_EFFECTIVE_DT_TM > ce.[EVENT_END_DT_TM] 
--use the report prompts as parameters for the ce.[EVENT_END_DT_TM] 
+0

你的標題說相交但你同時使用了交叉和工會的所有標籤。 「intersect」和「union all」的用法在SQL書籍聯機(https://msdn.microsoft.com/en-us/library/ff848745.aspx)中。或者你是否要求特別的東西? –

+0

我相信我應該更多地在交叉查詢的行上來獲取我需要的輸出。只是不知道如何創建上述查詢的語法。 – Tony77

+0

問題是什麼?你嘗試了什麼?或者你只是不想自己做? – Philipp

回答

0

一個交叉查詢的結構是簡單的:

select statement 1 
intersect 
select statement 2 
intersect 
select statement 3 
... 

此搜索將返回在所有列兩個選擇語句。 select語句中返回的列必須具有相同的數量和類型(或者至少可以轉換爲普通類型)。

您也可以使用內部聯接來執行相交類型的查詢,以過濾掉一個查詢中不在另一個查詢中的記錄。所以舉個簡單的例子,假設你有兩張顏色表。

Select distinct ColorTable1.Color 
from ColorTable1 
join ColorTable2 
on ColorTable1.Color = ColorTable2.Color 

這將返回ColorTable1這也是ColorTable2所有不同的顏色。使用連接進行過濾可以幫助您的查詢執行得更好,但它確實需要更多思考。

另見:Set Operators (Transact-SQL)

+0

謝謝布賴恩,你能否以正確的方式修改上面的結構? – Tony77