简体   繁体   English

如何按月计算列中的值?

[英]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.

相关问题 如何使用开始和结束日期计算每个月列中的数据数量 - how do i count the number of data in a column for each month using a start and end date 如何返回当前年份每个月的不同列值的总数,月份应作为列标题返回 - How can I return the total count of distinct column values for each month in current year, the months should return as column headers 如何使用Count来查找每个月的品牌 - how do I use Count to find brands in each month 如何计算联接表的每一行中值的出现次数? - How do I count the occurences of the values in each row of a joined table? 如何计算每个月在指定时间间隔内的列总数? - How to count the total of a column in a specified interval of time for each month? 我如何使用POSTGRESQL按月计算 - How do I count by month using POSTGRESQL 如何计算每列中每个不同值独立出现的次数? - How can I count how much each different values occur in each column independently? 如何根据日期在单独的列中显示月,月+ 1,月+2,月+3,月+4? - How do I display the Month, Month + 1, Month +2, Month +3, Month +4 in a separate column depending on the date? 如何选择彼此对应的列值? - How do I select column values to be corresponding to each other? 如何对每月SQL的列中的最大值进行分组 - How to group max values from column for each month SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM