Sql server 不重复的交叉查询

Sql server 不重复的交叉查询,sql-server,sql-server-2008,intersect,Sql Server,Sql Server 2008,Intersect,我已经多年没有大规模使用sql server了,我正在寻求帮助,了解如何正确使用sintax intersect类型查询来连接这两个数据集,而不是创建重复的名称。有些患者既有医嘱,也有临床事件条目,有些患者只有临床事件条目 数据集1 SELECT distinct ea.alias as FIN, per.NAME_Last + ', ' + per.NAME_FIRST + ' ' + Isnull(per.NAME_MIDDLE, '') as PatientNa

我已经多年没有大规模使用sql server了,我正在寻求帮助,了解如何正确使用sintax intersect类型查询来连接这两个数据集,而不是创建重复的名称。有些患者既有医嘱,也有临床事件条目,有些患者只有临床事件条目

数据集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]

intersect查询的结构如下所示:

select statement 1
intersect
select statement 2
intersect
select statement 3
...
这将返回两个select语句中的所有列。select语句中返回的列必须具有相同的数量和类型,或者至少可以转换为公共类型

您还可以只使用内部联接来过滤出一个查询中不在另一个查询中的记录,从而执行一种intersect类型的查询。举个简单的例子,假设你有两个颜色表

Select distinct ColorTable1.Color
from ColorTable1
join ColorTable2
on ColorTable1.Color = ColorTable2.Color
这将返回ColorTable1中与ColorTable2中相同的所有不同颜色。使用联接进行筛选可以帮助您的查询执行得更好,但这需要更多的考虑


另请参见:

您的标题是intersect,但您同时使用了intersect和union all标记。intersect和union的用法都在SQL联机丛书中。或者你是在具体要求什么?我认为我应该更多地关注intersect查询,以获得我需要的输出。只是不知道如何为上面的查询创建语法。问题是什么?你试了什么?或者你只是不想自己做?没有错误,因为不知道如何构造intersect命令和需求。谢谢Brian,你能以正确的方式修改上面的一个吗?