无重复项的交叉查询



我已经很多年没有大规模使用sql服务器了,并且寻求有关如何正确使用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]

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

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中的所有不同颜色。 使用联接进行筛选可以帮助查询更好地执行,但确实需要更多思考。

另请参阅:设置运算符 (Transact-SQL)

相关内容

  • 没有找到相关文章

最新更新