我的输入表patient_ID和Appt_Resource_ID(博士)(第二个表是刚开的患者姓名)查询返回不正确的结果
Patient_ID Appt_Resource_ID
88299 47
88299 1
88299 40
88299 40
88299 40
88299 40
我运行一个,就是写一个输出行为SQL patient_id和Doctor_ID发生的最多,在这种情况下是sb 40.但它正在输出Doctor_ID 1.其他情况下,我检查的操作是否正确。
这是查询:
select distinct A.Patient_id, P.Patient_name, b.Appt_resource_id
from [PM].[vwGenPatApptInfo] A
inner join
(
select top 100 percent patient_id, Appt_resource_id, count(Appt_resource_id) as DR_count,
row_number() over (partition by patient_id order by count(*) desc) as seqnum
from [PM].[vwGenPatApptInfo]
where Patient_ID is NOT NULL
group by patient_id,Appt_resource_id
order by patient_id, seqnum
) B on B.Patient_ID = A.Patient_ID
and B.seqnum = 1
inner join [PM].[vwGenPatInfo] P on A.Patient_id = P.Patient_id
where A.Appt_DateTime >= DATEADD(yyyy, -2, GETDATE()) and A.Appt_Cancelled_Date is NULL
但结果是这样的:
Patient_ID Appt_Resource_ID
88299 1