使用T-SQL查找百分比合规性

时间:2013-03-03 21:13:12

标签: sql-server sql-server-2008 tsql reporting-services reportingservices-2005

我不是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%。

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

1 个答案:

答案 0 :(得分: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

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