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

slysid

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.

Any suggestions, please.

Thanks

GMB

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 the order partition rows by month.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to calculate the previous month and year from string in Bash

calculate Age from year, month and day fields

Extracting year and month from datekey in my sql

parse year and month from a string SQL BigQuery

SQL : how to calculate an average from a query

How to get month name from date in Presto

How to calculate overdue fee's every month

how to calculate month difference from two different columns in Sql Developer?

How to extract day/month/year etc from varchar date field, using Presto?

How to cast unix timestamp as date and extract month from it in Presto SQL

Calculate the average of the last 12 months for every month?

Get start & end timestamp for month from Month and Year

Getting Year and Month from SQL

SUM from the first month of the year until selected month of the year SQL

How to start identity insert from 1 every finacial year?

Getting count of rows from every month of this year

How to get a half of every month from current year in Swift

How to calculate the difference of a month of the current year and same month from previous year in SQL

How to calculate daily average from aggregate results with SQL?

How to calculate the average year

How to get month and year from date in SQL

How to convert/extract from YYYYMM to year and name of Month? Teradata SQL

How to get year from timestamp in presto sql?

How to calculate monthly annual average from daily dataframe and plot it by abbreviated month

How to calculate year to date (YTD) of a financial year starting from month April using Pandas dataframe?

Calculate the same month from previous year

I need to calculate month-on-month change and change in same month last year from a table fetched from SQL database using python

Calculate birthdate from year, month and days

Athena calculate count per month from a date to current timestamp

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive