我在acess(2007)中有以下表格架构。
日期eventDate位(布尔)午餐, bit(boolean)snacks,bit(boolean)Tea,
我想有一个查询,给出一个月(使用eventdate)的luch,零食和茶(每列作为一列)的数量。
感谢您的帮助。
答案 0 :(得分:3)
在Access中,True为-1,False为0.因此,您可以使用这些值之和的绝对值来表示有多少是True。
SELECT Abs(Sum(lunch)) AS SumofLunch, Abs(Sum(snacks)) AS SumofSnacks, Abs(Sum(Tea)) AS SumofTea
FROM YourTable
WHERE eventDate >= #2009/08/01# And eventDate < #2009/09/01#;
答案 1 :(得分:2)
尝试:
SELECT SUM(ABS(lunch)) AS lunchCount, SUM(ABS(snacks)) AS snackCount, SUM(ABS(tea)) AS teaCount
FROM <TableName>
WHERE eventDate >= #1/1/2009# AND eventDate < #2/1/2009#
答案 2 :(得分:0)
我会在使用IIF语句的访问中攻击它(想想像其他数据库中的CASE语句)。 Access中的IIF在指定条件为真(第一个逗号之后)时返回一个值,如果它的计算结果为false(在第二个逗号之后)则返回另一个值。因为您正在使用布尔值,检查真实条件并通过汇总金额汇总将为您提供帮助。
SELECT Month(EventDate) AS TheMonth, Year(EventDate) AS TheYear,
SUM(IIF(Lunch, 1, 0)) AS LunchCount,
SUM(IIF(Snacks, 1, 0)) AS SnackCount,
SUM(IIF(Tea, 1, 0)) AS TeaCount
FROM YourTable
GROUP BY Month(EventDate), Year(EventDate)
ORDER BY Month(EventDate), Year(EventDate)
上述查询当然会返回所有月份和年份的计数,但如果需要,您可以轻松输入如下所示的日期范围。
SELECT Month(EventDate) AS TheMonth, Year(EventDate) AS TheYear,
SUM(IIF(Lunch, 1, 0)) AS LunchCount,
SUM(IIF(Snacks, 1, 0)) AS SnackCount,
SUM(IIF(Tea, 1, 0)) AS TeaCount
FROM YourTable
WHERE EventDate BETWEEN #1/1/2009# AND #1/31/2009#
GROUP BY Month(EventDate), Year(EventDate)
ORDER BY Month(EventDate), Year(EventDate)