简体   繁体   English

如何从 Presto 的 SQL (Athena) 中从一年开始计算每个月的平均值?

[英]How to calculate average for every month from start from year in Presto's SQL (Athena)?

Below is an example of the table data I have以下是我拥有的表格数据的示例

| date       | value |
| 2020-01-01 |  20   |
| 2020-01-14 |  10   |
| 2020-02-02 |  30   |
| 2020-02-11 |  25   |
| 2020-02-25 |  25   |
| 2020-03-13 |  34   |
| 2020-03-21 |  10   |
| 2020-04-06 |  55   |
| 2020-04-07 |  11   |

I would like to generate a result set as below我想生成如下结果集

| date       | value |  average                       |
| 2020-01-01 |  20   |  Jan average                   |
| 2020-01-14 |  10   |  Jan average                   |   
| 2020-02-02 |  30   |  Jan & Feb average             |
| 2020-02-11 |  25   |  Jan & Feb average             |
| 2020-02-25 |  25   |  Jan & Feb average             |
| 2020-03-13 |  34   |  Jan & Feb & Mar average       |
| 2020-03-21 |  10   |  Jan & Feb & Mar average       |
| 2020-04-06 |  55   |  Jan & Feb & Mar & Apr average |
| 2020-04-07 |  11   |  Jan & Feb & Mar & Apr average |

I tried to use window function OVER() and PARTITION() but I managed to get average on month by month rather than starting from the year.我尝试使用 window function OVER() 和 PARTITION() 但我设法逐月获得平均值,而不是从一年开始。

Any suggestions, please.请有任何建议。

Thanks谢谢

Not sure I understand your question, but if all you want is a running average for each row bound by year:不确定我是否理解您的问题,但如果您想要的只是逐年限制的每一行的运行平均值:

SELECT date, value, (
  SELECT AVG(value)
  FROM data ds
  WHERE ds.date <= d.date AND YEAR(ds.date) = YEAR(d.date)
) average
FROM data d
ORDER BY d.date ASC;

Example with MySQL (the syntax for this specific query is the same) MySQL 示例(此特定查询的语法相同)

If you want to include later rows of the same month in the average, use WHERE MONTH(ds.date) <= MONTH(d.date) .如果您想在平均值中包含同一个月的后续行,请使用WHERE MONTH(ds.date) <= MONTH(d.date)

This following query should give your expected output-以下查询应该给出您预期的输出 -

Demo Here 在这里演示

SELECT A.*,
(
    SELECT AVG(Value * 1.00) 
    FROM your_table B 
    WHERE YEAR(B.Date) = YEAR(A.DAte) 
    AND MONTH(B.Date) <= MONTH(A.DAte)
)
FROM your_table A

This query will make your output per year.此查询将使您的 output 每年。 But if you wants no partition by YEAR, just remove the YEAR filter from the sub query.但是,如果您不希望按 YEAR 进行分区,只需从子查询中删除 YEAR 过滤器。

This following query will return AVG with no consideration of YEAR, just AVG of all before months-以下查询将返回不考虑 YEAR 的 AVG,仅返回几个月前的 AVG-

Demo Here 在这里演示

SELECT A.*,
(
    SELECT AVG(Value * 1.00) 
    FROM your_table B 
    WHERE B.date <= 
    (
        SELECT MAX(Date) 
        FROM your_table C
        WHERE YEAR(c.Date) = YEAR(A.Date)
        AND MONTH(C.Date) = MONTH(A.Date)
    )
)
FROM your_table A

I think you want:我想你想要:

select 
    t.*,
    avg(value) over(
        partition by year(date)
        order by month(date)
    ) running_avg
from mytable t

This puts each year in a separate partition, and then orders partition rows by month.这会将每年放在一个单独的分区中,然后按月对分区行进行排序。

SELECT a.date,
    a.value,
     (Select avg(b.value) from myTable B where b.date < a.date and YEAR(a.date) = YEAR(b.date)) 
From myTable a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL中计算与今天日期的天、周、月、年差异? - How to Calculate Days,Weeks,Month,Year difference from Today's Date in SQL? 如何从SQL Server获取每年每月的计数记录? - How to get count record for every month for year from SQL Server? 如何根据SQL中的上个月平均值和去年同月平均值计算值 - How to calculate value based on average of previous month and average of same month last year in SQL 如何使用 Sql/Presto 从数字字符串中提取年份? - How to extract the year from a numeric string using Sql/Presto? 如何在 Presto SQL 中将 unix 时间戳转换为日期并从中提取月份 - How to cast unix timestamp as date and extract month from it in Presto SQL 如何计算 Athena (Presto) 中的总数百分比? - How to calculate percentage of total in Athena (Presto)? Presto SQL / Athena:如何仅返回小时、分钟和秒,从“间隔天到秒” - Presto SQL / Athena: how to return only hours, minutes and seconds, from "interval day to second" 如何使用 Presto 从 varchar 日期字段中提取日/月/年等? - How to extract day/month/year etc from varchar date field, using Presto? 如何从SQL中的日期获取月份和年份 - How to get month and year from date in SQL 如何从t-sql中获取过去一年中每个月的数据,每个月应该在不同的列中 - how can I get data from every month from past one year in t-sql and each month should be in different column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM