Access 2007 SQL Count

时间:2013-03-05 17:11:11

标签: sql-server ms-access-2007

我有一张这样的表

Days taken
12
10
10
12
9
10
8
10

我想要一个像这样放置的查询

Type          | Count
Days over 10  |   2
Days under 10 |   6

我到目前为止

SELECT Sum(IIf([table].[days taken]<=10,1,0)) AS [Days Under Ten], 
Sum(IIf([table].[days taken]>10,1,0)) AS [Days Over Ten]
FROM [table];

这给了我这个

Days under ten | Days over ten
6              |       2

但它不是我想要的,请帮助我。

1 个答案:

答案 0 :(得分:2)

您当前的查询非常接近正确的版本。您将需要使用:

SELECT 
  IIF([days taken] <=10, 'Days under 10', 'Days over 10') as Type,
  count([days taken]) as Total
FROM yourtable
group by IIF([days taken] <=10, 'Days under 10', 'Days over 10') 

您会看到我使用IIF分配Days over 10Days under 10。然后,您将相同的IIF添加到GROUP BY以获取count()