我需要帮助才能找到每月购买的汽车总数。我有这样的表(购买)设置(例如简化):
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
它产生了错误的报告。
答案 0 :(得分:3)
也许是这样的:
SELECT
COUNT( `country` ) ,
`model` ,
MONTH(purchase_date) AS Month
FROM
charmy_purchase
WHERE
`country` != ''
GROUP BY
model,
MONTH(purchase_date)
修改强>
如果你有不同的年份。那么你也可以考虑对年份进行分组。像这样:
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)
有用的参考资料: