在多个联接的查询中选择具有相同记录ID的第一条记录

时间:2019-03-25 20:43:15

标签: sql-server

我写了一个包含多个join语句的查询,并且只想选择顶部有多个相同PT_FIN的“ PT_FIN”。我在如何嵌入仅在我已编写的代码中选择一个PT_FIN的代码时遇到问题。

我找到了一些仅在选择第一行时提供答案的链接,但是就像我说的那样,我在使这些答案在我的代码中起作用时遇到了问题:

Select the first instance of a record

Selecting the first record out of each nested grouped record

USE EMTCQIData

DECLARE @StartDate Date
DECLARE @EndDate Date

Set @StartDate = '03/01/2018'
Set @EndDate = '03/25/2018'


Select ORD.PT_FIN, NOTE.Tracking_GROUP, NOTE.AUTHOR, 
FORMAT(ORD.CHECKIN_DT_TM, 'MM/dd/yyyy') as DOV, ORD.Order_Mnemonic, 
ORD.order_status,

CASE WHEN ORDER_MNEMONIC LIKE '%ketamine%' THEN 'YES' ELSE 'NO' END 
[KETAMINE ORDERED], ORD.DOSE,


CASE
       WHEN NOTE.RESULT LIKE '%99143%' THEN 'YES'
       ELSE 'NO'
END BILLED_SEDATION,


CASE
   WHEN NOTE2.RESULT LIKE '%Sedation%' THEN 'YES' Else 'NO' END 
 POWERNOTE_SEDATION

FROM [ED_Orders_Import_Master] AS ORD

INNER JOIN 

(
Select *
FROM [ED_NOTES_MASTER] AS NOTE
Where RESULT_TITLE_TEXT = 'ED Physician Charges' AND RESULT_DT_TM > 
@StartDate and RESULT_DT_TM < @EndDate

)
as NOTE ON NOTE.PT_FIN = ORD.PT_FIN

INNER JOIN 
(

Select *
FROM [ED_NOTES_MASTER] AS NOTE2
Where NOTE_TYPE like '%PowerNote ED%' AND RESULT_DT_TM > @StartDate and 
RESULT_DT_TM < @EndDate
)
as NOTE2 ON NOTE2.PT_FIN = ORD.PT_FIN
WHERE [Checkin_dt_tm] > @StartDate and [Checkin_dt_tm] < @EndDate AND 
ORDER_MNEMONIC LIKE '%ketamine%' and ORIG_ORD_AS like '%Normal%' and 
ORDER_STATUS like '%complete%'
ORDER by ORD.PT_FIN

我希望结果看起来像这样:

PT_FIN     Order       Billed Sedation       Power Note Sedation 
1         Ketamine         yes                     Yes
2         Ketamine         yes                     no 
3         Ketamine         yes                     Yes

1 个答案:

答案 0 :(得分:1)

该问题中缺少数据库模式和示例输入,但这可以是一种方法,因为您只需要验证注释的存在,我认为存在可以解决您的问题。如果您需要从相关子查询中获得多个值

,则Apply也会有所帮助
DECLARE @StartDate Date
DECLARE @EndDate Date

Set @StartDate = '03/01/2018'
Set @EndDate = '03/25/2018'


Select ORD.PT_FIN, ORDER_MNEMONIC, 
CASE WHEN EXISTS(SELECT 1 FROM [ED_NOTES_MASTER] WHERE RESULT_TITLE_TEXT = 'ED Physician' AND PT_FIN = ORD.PT_FIN AND RESULT LIKE '%99143%') THEN 'Yes' ELSE 'NO' END AS BILLED_SEDATION,
CASE WHEN EXISTS(SELECT 1 FROM [ED_NOTES_MASTER] WHERE NOTE_TYPE like '%PowerNote ED%' PT_FIN = ORD.PT_FIN AND RESULT LIKE '%Sedation%') THEN 'Yes' ELSE 'NO' END AS POWER_NOTE_SEDATION
FROM [ED_Orders_Import_Master] AS ORD
WHERE [Checkin_dt_tm] > @StartDate and [Checkin_dt_tm] < @EndDate AND 
ORDER_MNEMONIC LIKE '%ketamine%' and ORIG_ORD_AS like '%Normal%' and 
ORDER_STATUS like '%complete%'
ORDER by ORD.PT_FIN

仅在第一个情况下使用申请获得注释,我得到了您当前的方法,并将您的INNER JOINs转换为TOP 1的OUTER APPLYs来完成标题中要求的内容

Select ORD.PT_FIN, NOTE.Tracking_GROUP, NOTE.AUTHOR, 
FORMAT(ORD.CHECKIN_DT_TM, 'MM/dd/yyyy') as DOV, ORD.Order_Mnemonic, 
ORD.order_status,

CASE WHEN ORDER_MNEMONIC LIKE '%ketamine%' THEN 'YES' ELSE 'NO' END 
[KETAMINE ORDERED], ORD.DOSE,


CASE
       WHEN NOTE.RESULT LIKE '%99143%' THEN 'YES'
       ELSE 'NO'
END BILLED_SEDATION,


CASE
   WHEN NOTE2.RESULT LIKE '%Sedation%' THEN 'YES' Else 'NO' END 
 POWERNOTE_SEDATION

FROM [ED_Orders_Import_Master] AS ORD

OUTER APPLY
(
Select TOP 1 *
FROM [ED_NOTES_MASTER] 
Where  PT_FIN = ORD.PT_FIN
ANd RESULT_TITLE_TEXT = 'ED Physician Charges' AND RESULT_DT_TM > 
@StartDate and RESULT_DT_TM < @EndDate
ORDER BY RESULT_DT_TM
)
as NOTE
OUTER APPLY
(
Select TOP 1 *
FROM [ED_NOTES_MASTER]
Where PT_FIN = ORD.PT_FIN NOTE_TYPE like '%PowerNote ED%' AND RESULT_DT_TM > @StartDate and 
RESULT_DT_TM < @EndDate
ORDER BY RESULT_DT_TM
)
WHERE [Checkin_dt_tm] > @StartDate and [Checkin_dt_tm] < @EndDate AND 
ORDER_MNEMONIC LIKE '%ketamine%' and ORIG_ORD_AS like '%Normal%' and 
ORDER_STATUS like '%complete%'
ORDER by ORD.PT_FIN