【问题标题】:how many trainings are started in any given month SQL在任何给定月份 SQL 中开始了多少次培训
【发布时间】:2021-02-12 08:01:25
【问题描述】:
我有数据集:
名称:许可证
id licence type start
1 1 training 12/08/2017
2 2 training 17/08/2017
我需要计算每个月使用了多少许可证
我试过这个:
SELECT COUNT(type)
FROM Licences
GROUP BY MONTH(start)
但我只得到全年所有记录的数量,而不是每个月
【问题讨论】:
标签:
sql
postgresql
datetime
count
【解决方案1】:
我想您也应该将其用作列,而不仅仅是在组条件中:
SELECT
date_trunc('month',start) as "month",COUNT(type) as "license_count"
FROM Licences
GROUP BY date_trunc('month',start)
ORDER BY date_trunc('month',start)
【解决方案2】:
这对我有用:
SELECT COUNT([type]) as count_types, MONTH([start]) as type_month FROM Licences GROUP BY MONTH([start])