相交查询没有重复

时间:2016-03-09 22:32:05

标签: sql-server sql-server-2008 intersect

我多年没有使用大型复杂规模的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]

1 个答案:

答案 0 :(得分:0)

交叉查询的结构非常简单:

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

这将返回两个select语句中的所有列。 select语句中返回的列必须具有相同的数量和类型(或者至少可以转换为通用类型)。

您也可以使用内部联接来执行交叉类型的查询,以过滤掉一个查询中不在另一个查询中的记录。因此,举一个简单的例子,假设你有两张颜色表。

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

这将返回ColorTable1中同样位于ColorTable2的所有不同颜色。使用联接进行过滤可以帮助您的查询更好地执行,但需要更多考虑。

另见:Set Operators (Transact-SQL)