0

我需要帮助来查找每月购买的汽车总数。我有一个像这样的表(购买)设置(例如简化):

  sno
  country
  purchased_date(yyyy-mm-dd)
  make
  model

现在我想生成一份包含每月购买汽车总数的报告,这是我的查询:

SELECT COUNT(`country`), `model`, `make`, `purchase_date`, `country`
FROM charmy_purchase 
WHERE `country` != '' 
GROUP BY model

它产生错误的报告。

4

1 回答 1

3

Maybe something like this:

SELECT 
    COUNT( `country` ) , 
    `model` , 
    MONTH(purchase_date) AS Month
FROM 
    charmy_purchase 
WHERE 
    `country` != '' 
GROUP BY 
   model,
   MONTH(purchase_date)

EDIT

If you have different years. Then you might consider grouping on year as well. Like this:

SELECT 
    COUNT( `country` ) , 
    `model` , 
    MONTH(purchase_date) AS Month,
    YEAR(purchase_date) AS year
FROM 
    charmy_purchase 
WHERE 
    `country` != '' 
GROUP BY 
   model,
   MONTH(purchase_date),
   YEAR(purchase_date)

Useful references:

于 2012-05-08T12:19:15.500 回答