我希望看到小部件在我的Postgres数据库中获得created_at
的最常见的一周。
这是我所拥有的,但它返回平均值(3.744 ...),而不是表示一周中最常见日的整数。有谁知道我怎么能改善这个?
SELECT AVG(start_day) AS start_day
FROM (SELECT date_part('dow', created_at) AS start_day
FROM widgets) TMP;
答案 0 :(得分:1)
SELECT date_part('dow', created_at) AS start_day
FROM widgets
GROUP BY date_part('dow', created_at)
ORDER BY COUNT(*) DESC
LIMIT 1
答案 1 :(得分:1)
SELECT start_day, count(*)
FROM (
SELECT date_part('dow', created_at) AS start_day
FROM widgets) sub
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;