[英]How do I count the values in a column by each month?
In my data frame, I am trying to count number of values of a column per month, given by the time column in SQL.在我的数据框中,我试图计算每月列的值数,由 SQL 中的时间列给出。 I want the output to have a count of the number of values in a column for each month.我希望 output 对每个月的列中的值数进行计数。 I know I can use the where function to count for one month and I could do this for all 12 months, but was wondering if there was a more efficient way.我知道我可以使用 where function 来计算一个月,我可以在所有 12 个月内都这样做,但想知道是否有更有效的方法。 Here's the inefficient example:这是效率低下的例子:
SELECT
count(column1) AS Total
FROM DataFrame
WHERE MONTH(date) == 1
GROUP by column1 ORDER by count(column1) DESC LIMIT 10
I am trying to count distinct values of a column per month, given by the time column in SQL.我试图计算每个月列的不同值,由 SQL 中的时间列给出。 I我
You seem to want GROUP BY
:你似乎想要GROUP BY
:
SELECT MONTH(date) as mon, approx_count_distinct(column1, 0.1) AS Total
FROM DataFrame
GROUP by MONTH(date)
ORDER by Total DESC
LIMIT 10;
Note that using MONTH()
without YEAR()
or a filter on the date is highly suspicious.请注意,使用没有YEAR()
的MONTH()
) 或在日期上使用过滤器是非常可疑的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.