【发布时间】:2016-06-07 21:42:01
【问题描述】:
MY 输入表是 Patient_ID 和 Appt_Resource_ID (Doctor)(第二个表只是获取患者姓名)
Patient_ID Appt_Resource_ID
88299 47
88299 1
88299 40
88299 40
88299 40
88299 40
我正在运行一个 sql,旨在为患者 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
【问题讨论】:
标签: sql sql-server