我需要一种方法来计算患者的代码组合,所以我需要根据这份报告了解每个服务计划协议的总持续时间,所以基于下图我需要它像这样
患者:00000036
成人CSS IND 240
成人OP IND 120
患者:00000040
成人CSS IND 420
成人OP IND 60
我正在使用Microsoft SQL Server 2005,我更喜欢在SQL中修复,但如果它可以在水晶中完成,我可以使用它。先感谢您。
select
pct.patient_id,
pct.clinic_id,
pct.service_id,
pct.program_id,
pct.protocol_id,
pct.proc_duration
from patient_clin_tran pct
join patient p
on pct.patient_id = p.patient_id and pct.episode_id = p.episode_id
join patient_custom pc
on pct.patient_id = pc.patient_id
join staff s
on pct.attending_id = s.staff_id
where pc.health_home = 'Y'
group by pct.patient_id, pct.clinic_id, pct.service_id, pct.program_id, pct.protocol_id, pct.proc_duration
order by pct.patient_id, pct.clinic_id, pct.service_id, pct.protocol_id
答案 0 :(得分:1)
你很亲密。您确实需要GROUP BY
,但您不希望GROUP BY
持续时间列。相反,您希望使用SUM
列SELECT
列表中的proc_duration
功能:
select
pct.patient_id,
pct.clinic_id,
pct.service_id,
pct.program_id,
pct.protocol_id,
SUM(pct.proc_duration) AS [Total Duration]
FROM patient_clin_tran pct
join patient p
on pct.patient_id = p.patient_id and pct.episode_id = p.episode_id
join patient_custom pc
on pct.patient_id = pc.patient_id
join staff s
on pct.attending_id = s.staff_id
where pc.health_home = 'Y'
group by pct.patient_id, pct.clinic_id, pct.service_id, pct.program_id, pct.protocol_id
order by pct.patient_id, pct.clinic_id, pct.service_id, pct.program_id, pct.protocol_id