1

这是这里查询的扩展。我有一个当前正确的结果集,如下所示:

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

内科医学数字都没有正确显示。

4

1 回答 1

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'

通过在两个列选择语句中将 pv.spclty_desc 更改为 p.spclty_desc,问题得到了解决。然而,这并没有告诉我为什么在声明中只有内科失败,而其他两个没有失败。

于 2013-07-03T12:44:13.097 回答