COUNT针对各自类型销售的物业数量?

时间:2014-02-15 15:24:10

标签: sql function count ms-access-2007

我对SQL很陌生,我正在试图弄清楚如何得到这个结果:

Type                   Number Sold
Detached                    5
Semi-detached               2
Terrace                     1
Link                        8

使用查询,我尝试过:

  SELECT SPropertyType AS Type, COUNT (SPropertyCurrentState) AS NumberSold
  FROM SaleProperty
  WHERE SPropertyCurrentState = 'Sold';

但它给了我一个错误。

2 个答案:

答案 0 :(得分:0)

您忘了使用GROUP BY。 使用此:

SELECT
    SPropertyType AS Type,
    COUNT (SPropertyCurrentState) AS NumberSold
FROM
    SaleProperty
WHERE
    Upper(SPropertyCurrentState) = 'SOLD'
GROUP BY
    SPropertyType;

答案 1 :(得分:-1)

您需要添加group by SPropertyType子句

最终的SQL将如下所示

 SELECT 
    SPropertyType AS Type, 
    COUNT (SPropertyCurrentState) AS NumberSold
  FROM SaleProperty
  WHERE 
    SPropertyCurrentState = 'Sold'
  GROUP BY 
    SPropertyType;