按日期的SQL百分比计数查询

时间:2012-09-18 20:11:55

标签: sql ms-access-2007

我可以使用以下方法计算Microsoft Access 2007 SQL查询中特定日期的百分比计数:

SELECT Date, Val, (Count(Val) / (SELECT Count(*) From Table HAVING Date=#7/31/2012#) as PercentVal    
FROM Table  
GROUP BY Date, Val  
HAVING Date=#7/31/2012#

但是,我想使用计数总计对每个日期进行相同的计算。例如,查询:

SELECT Date, Val, Count(*) AS CountVal  
FROM Table  
GROUP BY Date, Val  

查找每个时期的计数。我想添加一个包含百分比计数的附加列。但是,我似乎无法弄清楚如何在不使用上述文本块的情况下计算每个时期的计数百分比,并为每个时期设置查询。

1 个答案:

答案 0 :(得分:1)

您可以这样子查询:

SELECT A.ADate, A.Val, COUNT(A.Val) / B.DateCount
FROM Table1 AS A
INNER JOIN (
    SELECT C.ADate, COUNT(*) AS DateCount
    FROM Table1 C
    GROUP BY C.ADate
) AS B ON A.ADate = B.ADate
GROUP BY A.ADate, A.Val, B.DateCount