2

我不是 T-SQL 方面的专家,所以在这里我试图找到医务人员主管对流感疫苗、结核病测试和呼吸器测试的依从性百分比。每个员工都有一个与其员工信息相关联的主管姓名。下面的代码工作正常,它为我提供了上述测试的 %。问题是我想通过主管获取 ID、姓名和部门以及 % 合规性。

预期的输出是这样的:

Supervisor        ID      NAME     Dept      %Flu     %TB    %FIT
Elaine Jong                                   98%    100%     52%
                001       MARY   SURGERY    
                002       SUSAN  SURGERY
James Ande                                   100%     98%     78%
                267       JIM    INPATIENT
                789       SAM    INPATIENT

Current OUTPUT
                 %Flu     %TB    %FIT
Elaine Jong      98%    100%     52%
James Ande      100%     98%     78%

和查询:

SELECT E.FLDSUPRNAME AS Supervisor,

1.0*SUM(
     CASE WHEN I.FLDDATE IS NULL 
          THEN 0 ELSE 1 
     END)/SUM(1)    AS Percent_Flu_Compliant,

1.0*SUM(
    CASE WHEN F.FLDDATE IS NULL OR (F.FLDDATE+365) < GETDATE() 
         THEN 0 ELSE 1  
     END) / SUM(1)  
AS Percent_Fit_Compliant,

1.0*SUM(
    CASE WHEN PPDx.FLDDATEDUE IS NULL 
              AND TBSSx.FLDDATEDUE IS NULL
              AND CDUEx.FLDDATEDUE IS NULL 
        THEN 1 ELSE 0 
    END) /SUM(1)   AS Percent_TB_Compliant

FROM   EMPLOYEE E

LEFT OUTER JOIN DEPT D
ON D.FLDCODE= E.FLDDEPT



LEFT OUTER JOIN IMMUNE I ON I.FLDEMPLOYEE = E.FLDREC_NUM AND I.FLDTYPE IN ('109', '111')  
AND I.FLDDATE = ( SELECT MAX(FLDDATE) FROM IMMUNE I2 WHERE E.FLDREC_NUM = I2.FLDEMPLOYEE 
AND I2.FLDTYPE IN ('109','111') ) AND I.FLDDATE >= @Flu_Date AND I.FLDDATE <= GETDATE()


LEFT OUTER JOIN FITTEST F ON E.FLDREC_NUM = F.FLDEMPLOYEE
AND F.FLDDATE = (SELECT MAX(FLDDATE) FROM FITTEST F2 WHERE E.FLDREC_NUM = F2.FLDEMPLOYEE) 

LEFT OUTER JOIN  REQEXAM PPDx
ON PPDx.FLDEMPLOYEE = E.FLDREC_NUM
AND PPDx.FLDPHYSICAL = '110' AND
PPDx.FLDDATEDUE <= getdate()

LEFT OUTER JOIN  REQEXAM PPDL
ON PPDL.FLDEMPLOYEE = E.FLDREC_NUM
AND PPDL.FLDPHYSICAL = '110' 

LEFT OUTER JOIN  REQEXAM TBSSx
ON TBSSx.FLDEMPLOYEE = E.FLDREC_NUM
AND TBSSx.FLDPHYSICAL = 'TBSS' AND
TBSSx.FLDDATEDUE <= getdate()

LEFT OUTER JOIN  REQEXAM TBSSL
ON TBSSL.FLDEMPLOYEE = E.FLDREC_NUM
AND TBSSL.FLDPHYSICAL = 'TBSS'


LEFT OUTER JOIN  REQEXAM CDUEx
ON CDUEx.FLDEMPLOYEE = E.FLDREC_NUM
AND CDUEx.FLDPHYSICAL = '109' AND
CDUEx.FLDDATEDUE <= getdate()

LEFT OUTER JOIN EMP S
ON S.FLDREC_NUM = E.FLDREC_NUM

WHERE  E.FLDCOMP = @company
   AND E.FLDSTATUS = 'A'
   AND E.FLDSUPRNAME <> ' '
   AND E.FLDID <> ' ' 

GROUP BY E.FLDSUPRNAME

ORDER BY    E.FLDSUPRNAME

如果我在 select 和 group by 上添加 ID、NAME 和 Dept,SUM(1) 将变为 1 或 0,所以我得到所有主管的 100% 或 0%。

对此的任何帮助都非常感谢。谢谢你的时间。

4

1 回答 1

0

使用 UNION,将空白列添加到您的第一个查询并通过以下方式删除订单:

SELECT (CASE WHEN ID IS NULL THEN Supervisor ELSE '' END) ,ID, name,dept,Percent_Flu_Compliant,Percent_TB_Compliant,Percent_Fit_Compliant FROM
(
SELECT E.FLDSUPRNAME AS Supervisor, NULL as ID, NULL as    name, NULL as dept
(...)
GROUP BY hiddensupervisor, Supervisor, ID, name, dept
UNION ALL
SELECT E.FLDSUPRNAME  Supervisor, E.id, E.name, E.dept,    NULL as Percent_Flu_Compliant, NULL as Percent_TB_Compliant, NULL asPercent_Fit_Compliant
FROM Employee
) as q
ORDER BY supervisor, (CASE WHEN ID IS NULL THEN 1 ELSE 0 END),ID

我们添加了隐藏的主管列,以便能够将员工置于其主管之下,但将该字段留空(我们也无法在外部查询中添加它和用例,不知道哪个更快)。显然我们必须尝试案例

于 2013-03-04T00:45:17.990 回答