比较SQL中两个不相关的计数

时间:2014-06-17 18:31:50

标签: sql ms-access

我有两个布尔值,我想将它们的计数分开。

假设一个表在X列中有1个是值,在Y列中有2个是值。 我希望SELECT Count(X)/ Count(Y)返回.5。

我在网上找到的解决方案使用WITHMS-Access s不允许我使用。

以下是我使用WITH进行更多说明的内容:

WITH T2 AS
(SELECT GRANTS.[Grant Program Number] AS GPN, Count(GRANTS.[Grant Awarded]) AS AwardCount
FROM GRANTS
WHERE GRANTS.[Grant Awarded]=Yes)
SELECT (T2.AwardCount) / (T1.SubCount) AS [Award Rate]
FROM (SELECT GRANTS.[Grant Program Number] AS GPN, Count(GRANTS.[Grant Submitted]) AS SubCount 
FROM GRANTS 
WHERE GRANTS.[Grant Submitted]=Yes) AS T1 INNER JOIN GRANTS ON T1.GPN = T2.GPN
GROUP BY [Award Rate];

2 个答案:

答案 0 :(得分:0)

免责声明,我没有访问权限,因此可能无效。也许无论如何它会给你一些想法:

select (1.0 * count( case when X = 'yes' then 1 end )) 
            / count( case when Y = 'yes' then 1 end ) 
from T;

如果X和Y是布尔值:

select (1.0 * count( case when X then 1 end )) 
            / count( case when Y then 1 end ) 
from T;

你可以试试这个:

select (1.0 * sum( IIf(X, 1, 0))) 
            / sum( IIf(Y, 1, 0)) 
from T;

答案 1 :(得分:0)

如果您正在寻找整个表的奖励率,那么查询将是

SELECT
    SUM(IIF([Grant Awarded],1,0))/ NZ(SUM(IIF([Grant Submitted],1,0)),1) as [Award Rate]
FROM  
    GRANTS