这是此查询here的扩展。我有一个当前正确的结果集,看起来像这样:
MD Name | # PTS | Med Staff | AVG LOS | AVG Bench LOS | AVG DIFF
MCP | 4 | Ortho SX | 8.000000| 2.650000 | 5.350000
我想补充一下这个结果,看到该部门的患者总数和相同的平均值,但是对于整个部门而言。通过执行以下操作,我能够使每个部门的总患者得到正确的总结,因为上面的原始查询中添加了另一列。
(SELECT
COUNT(DISTINCT V.PT_ID)
FROM smsdss.pract_dim_v p
JOIN smsmir.vst_rpt v
on v.adm_pract_no = p.src_pract_no
WHERE pv.med_staff_dept = p.med_staff_dept
AND v.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND v.vst_type_cd = 'I'
AND p.spclty_desc != 'NO DESCRIPTION'
--AND pv.spclty_desc NOT LIKE 'HOSPITALIST%'
AND v.drg_std_days_stay IS NOT NULL
AND p.pract_rpt_name != '?'
AND p.orgz_cd = 's0x0'
AND p.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
) AS '# PTS For Dept'
我手动检查了总和,它们是正确的。我知道想在p.med_staff_dept上添加AVG LOS,AVG BENCH LOS和AVG DIFF。这是我开始获得wack-a-doo(技术术语)结果的地方。
以下是我尝试获得每个部门的AVG LOS的部分:
(SELECT
AVG(v.len_of_stay)
FROM smsdss.pract_dim_v p
JOIN smsmir.vst_rpt v
ON v.adm_pract_no = p.src_pract_no
WHERE pv.med_staff_dept = p.med_staff_dept
AND v.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND v.vst_type_cd = 'I'
AND p.spclty_desc != 'NO DESCRIPTION'
--AND pv.spclty_desc NOT LIKE 'HOSPITALIST%'
AND v.drg_std_days_stay IS NOT NULL
AND p.pract_rpt_name != '?'
AND p.orgz_cd = 's0x0'
AND p.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
) AS 'DEPT ALOS'
我的两个小组在针对excel检查时获得正确的数字,但中间的行组非常不正确,如下所示:
# PTS FOR DEPT | DEPT ALOS
330 | 5.284848 <-- SAME IN EXCEL
737 | 4.952510 <-- EXCEL SHOWS 196 AND 6.214285
111 | 3.072072 <-- SAME IN EXCEL
这是我的查询的FROM子句:
FROM smsmir.vst_rpt vr
JOIN smsdss.pract_dim_v pv
ON vr.adm_pract_no = pv.src_pract_no
WHERE vr.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND vr.vst_type_cd = 'I'
AND pv.spclty_desc != 'NO DESCRIPTION'
--AND pv.spclty_desc NOT LIKE 'HOSPITALIST%'
AND vr.drg_std_days_stay IS NOT NULL
AND pv.pract_rpt_name != '?'
AND pv.orgz_cd = 's0x0'
AND pv.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
GROUP BY pv.pract_rpt_name, pv.med_staff_dept, pv.spclty_desc
ORDER BY pv.med_staff_dept, AVG(vr.len_of_stay - vr.drg_std_days_stay)DESC
期望的最终输出:
MD NAME | # PTS | MED STAFF | AVG LOS | AVG BENCH | AVG DIFF | DEPT TOT | AVG LOS | AVG BNCH | AVG DIFF
MCP | 4 | Ortho SX | 8.000000| 2.650000 | 5.350000 | 300 | 4.990000| 4.500000 | 0.490000
完整代码:
DECLARE @STARTDATE DATETIME
DECLARE @ENDATE DATETIME
SET @STARTDATE = '2013-05-01'
SET @ENDATE = '2013-05-31'
SELECT DISTINCT pv.pract_rpt_name AS 'PHYSICIAN'
, COUNT(DISTINCT vr.pt_id) AS '# PTS'
--, pv.spclty_desc AS 'SPECIALTY'
, pv.med_staff_dept AS 'MED STAFF'
, AVG(vr.len_of_stay) AS 'AVG LOS'
, AVG(vr.drg_std_days_stay) AS 'AVG DRG LOS BENCH'
, AVG(vr.len_of_stay - vr.drg_std_days_stay) AS 'OPPORTUNITY'
, (SELECT
COUNT(DISTINCT V.PT_ID)
FROM smsdss.pract_dim_v p
JOIN smsmir.vst_rpt v
on v.adm_pract_no = p.src_pract_no
WHERE pv.med_staff_dept = p.med_staff_dept
AND v.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND v.vst_type_cd = 'I'
AND p.spclty_desc != 'NO DESCRIPTION'
AND pv.spclty_desc NOT LIKE 'HOSPITALIST%'
AND v.drg_std_days_stay IS NOT NULL
AND p.pract_rpt_name != '?'
AND p.orgz_cd = 's0x0'
AND p.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
) AS '# PTS For Dept'
-- currently not working properly
, (SELECT
AVG(V.len_of_stay)
FROM smsmir.vst_rpt v
JOIN smsdss.pract_dim_v p
ON v.adm_pract_no = p.src_pract_no
WHERE pv.med_staff_dept = p.med_staff_dept
AND v.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND v.vst_type_cd = 'I'
AND p.spclty_desc != 'NO DESCRIPTION'
AND pv.spclty_desc NOT LIKE 'HOSPITALIST%'
AND v.drg_std_days_stay IS NOT NULL
AND p.pract_rpt_name != '?'
AND p.orgz_cd = 's0x0'
AND p.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
) AS 'DEPT ALOS'
FROM smsmir.vst_rpt vr
JOIN smsdss.pract_dim_v pv
ON vr.adm_pract_no = pv.src_pract_no
WHERE vr.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND vr.vst_type_cd = 'I'
AND pv.spclty_desc != 'NO DESCRIPTION'
AND pv.spclty_desc NOT LIKE 'HOSPITALIST%'
AND vr.drg_std_days_stay IS NOT NULL
AND pv.pract_rpt_name != '?'
AND pv.orgz_cd = 's0x0'
AND pv.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
GROUP BY pv.pract_rpt_name, pv.med_staff_dept, pv.spclty_desc
ORDER BY pv.med_staff_dept, AVG(vr.len_of_stay - vr.drg_std_days_stay)DESC
没有一个内科数字正确出现。
答案 0 :(得分:1)
我发现的问题就在这里:
, (SELECT
COUNT(DISTINCT V.PT_ID)
FROM smsdss.pract_dim_v p
JOIN smsmir.vst_rpt v
on v.adm_pract_no = p.src_pract_no
WHERE pv.med_staff_dept = p.med_staff_dept
AND v.adm_dtime BETWEEN @STARTDATE AND @ENDATE
AND v.vst_type_cd = 'I'
AND p.spclty_desc != 'NO DESCRIPTION'
AND pv.spclty_desc NOT LIKE 'HOSPITALIST%' <-- should be p.spclty not pv.
AND v.drg_std_days_stay IS NOT NULL
AND p.pract_rpt_name != '?'
AND p.orgz_cd = 's0x0'
AND p.med_staff_dept IN (
'INTERNAL MEDICINE',
'FAMILY PRACTICE',
'SURGERY'
)
) AS '# PTS For Dept'
通过在两个列select语句中将pv.spclty_desc更改为p.spclty_desc,问题得以解决。然而,这并没有告诉我为什么在声明中只有Internal Medicinie失败而不是其他两个。