Nikhil.Patel Nikhil.Patel - 1 month ago 6
SQL Question

Year wise Average days SQL

Today i have below problem while perform an sql query. Please find below data.

enter image description here

I perform SQL query on my table and get the below resulted output. i perform Group by on ID, Name, Week, Year, Days now i want the Days column as average of All Days based on year column. means there is multiple value of year is exist so i need Avg of Days data in all rows of DAYS for particular row. expected result as per below.

enter image description here

Thanks in Advance!!!
Write in comment if you have any query.

Answer

You can use OVER:

SELECT
  *,
  AVG(Days) OVER (PARTITION BY LEFT(Year, 4)) AvgDays
FROM
  Tbl

Note: Just grouped by year (2016)

Comments