如何将案件的总数除以总数

时间:2018-08-22 16:56:38

标签: sql sql-server case-when

我有一个代码,可以在...的情况下按情况细分计数。但是,我需要第三列来获取whens进行计数并将结果除以原始总数的情况。...

我该怎么办?

代码在下面

基本上我需要

每个组的交易数量/交易总数

Select case when t_opportunity.opportunityID=yes.opportunityID 
            then 'Compliant' 
            else 'Non Compliant' 
       end as Compliance, 
       count(t_opportunity.opportunityID) as NumberOfDeals
from T_opportunity
left join (select t_O.opportunityId
           from t_opportunity T_O   
           inner join T_quote on T_O.opportunityID=t_quote.OpportunityId
           where t_quote.createddate<=(T_O.applicationsigneddate+7)
           group by t_O.opportunityID
           having count(t_quote.quoteID)>=3) yes 
  on T_opportunity.opportunityID=yes.opportunityID  
group by case when t_opportunity.opportunityID=yes.opportunityID 
              then 'Compliant' 
              else 'Non Compliant' 
         end

先谢谢您!

3 个答案:

答案 0 :(得分:0)

如果我正确理解,这可能对您有用。样本数据和预期结果始终对数据转换问题很有帮助。

这假设如果一个机会在“是”中有多个匹配项,则您希望它被计算两次(或N次)。如果不是这种情况,只需在其中插入一个与众不同的位置即可。

SELECT compliance,
       COUNT(1) AS NumberOfDeals,
       COUNT(1)/Total_Count AS FieldYouAreAskingFor  -- Cast accordingly to get precision. 
  FROM (SELECT o.opportunityID, -- Shove distinct here. Or better rewrite as EXIST if you only want to count multiple matches once.
               CASE WHEN yes.opportunityID IS NOT NULL THEN 'COMPLIANT'
                    ELSE 'NOT COMPLIANT'
                END AS Compliance,
               SUM(1) OVER() AS Total_Count
          FROM T_OPPORTUNITY O
          LEFT
          JOIN (select t_O.opportunityId
                   from t_opportunity T_O   
                   inner join T_quote on T_O.opportunityID=t_quote.OpportunityId
                   where t_quote.createddate<=(T_O.applicationsigneddate+7)
                   group by t_O.opportunityID
                   having count(t_quote.quoteID)>=3
               ) yes 
            ON o.opportunityID = yes.opportunityID
       ) TMP
 GROUP
    BY TMP.Compliance,
       TMP.Total_Count;

答案 1 :(得分:0)

我将推测您打算这样做:

select (case when t_opportunity.opportunityID = yes.opportunityID 
            then 'Compliant' 
            else 'Non Compliant' 
        end) as Compliance, 
       count(t_opportunity.opportunityID) as NumberOfDeals,
       count(t_opportunity.opportunityID) * 1.0 / sum(count(t_opportunity.opportunityID)) over () as Ratio

这使用窗口函数来计算行总数。

答案 2 :(得分:0)

如果我说对了,那么当您的机会在一周内至少创建3个报价时,它们就是“合规的”(无论如何,我认为您会明白的。)

您可以在一个查询中计算合规性:

SELECT o.opportunityID,
  CASE 
    WHEN COUNT(q.quoteID) >= 3
    THEN 'Compliant'
    ELSE 'Not Compliant'
  END AS Compliance
FROM T_opportunity o
  LEFT OUTER JOIN t_quote q 
    ON o.opportunityID = q.OpportunityId
    AND DATEADD(day, 7, o.applicationsigneddate) >= q.createddate
GROUP BY o.opportunityID

下一步,您可以将此结果用作按Compliance进行分组的输入:

WITH 
  OpportunityCompliance (opportunityID, Complicance) AS (
    SELECT o.opportunityID,
      CASE 
        WHEN COUNT(q.quoteID) >= 3
        THEN 'Compliant'
        ELSE 'Not Compliant'
      END
    FROM T_opportunity o
      LEFT OUTER JOIN t_quote q 
        ON o.opportunityID = q.OpportunityId
        AND DATEADD(day, 7, o.applicationsigneddate) >= q.createddate
    GROUP BY o.opportunityID
  )
SELECT 
  Complicance,
  COUNT(*) AS NumberOfDeals,
  1.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS Ratio
FROM OpportunityCompliance
GROUP BY Complicance