sql,条件计数不同

时间:2015-07-23 09:43:01

标签: sql sql-server count

我必须在一个不同的计数中做一个困难的过滤器。

数据类似

auditor - anomaly
Bob - s
Bob - n
Bob - n
Jon - n
Jon - n
Rob - s
Rob - n

我必须计算只有'

的人数

这个计数是复杂查询的一部分,所以我想找到一种在select语句中内联的方法:

SELECT
    SELECT      
            Office
        ,   SUM(OvverideRating)  AS [n. override]                   
        ,   COUNT(DISTINCT inspectorWithOverride) AS [n. auditor]
        ,   CAST(SUM(NonMotivato) AS FLOAT) / CAST(SUM(OvverideRating) AS FLOAT) [% on override]
        ,   COUNT(DISTINCT anomaly) AS [n. auditor with anomaly]
        ,   CAST(COUNT(DISTINCT anomaly) AS FLOAT) / CAST(COUNT(DISTINCT IspettoreConOverride) AS FLOAT) AS [% auditor with anomaly]    

FROM 
        (
        SELECT  DISTINCT
                Office  
            ,   activityID
            ,   CASE
                    WHEN [Override] ='S' THEN 1
                    ELSE 0
                END     AS OvverideRating
            ,   CASE
                    WHEN CAST(memCp_NoteTeamLeader AS NVARCHAR(MAX)) is not null AND [Override] ='S' THEN 1
                    ELSE 0
                END  AS Motivated
           ,    CASE
                    WHEN CAST(memCp_NoteTeamLeader AS NVARCHAR(MAX)) is null AND [Override] ='S' THEN 1
                    ELSE 0
                END AS NotMotivated
            ,   CASE
                    WHEN [Override] ='S' THEN strCb_IDIspettore
                    ELSE NULL
                END AS inspectorWithOverride
            ,   CASE
                    WHEN CAST(memCp_NoteTeamLeader AS NVARCHAR(MAX)) is null AND [Override] ='S' THEN strCb_IDIspettore
                    ELSE NULL
                END AS anomaly

        FROM ... complex subquery ...
        ) AS data
    GROUP BY 
        Office  

1 个答案:

答案 0 :(得分:1)

我是这样做的 我统计不同的审计员减去那些有“N”

的人数