I am trying to understand window functions in Snowflake better. I was pretty fine handling simple ones but I stumbled over something that I don't get.
In below code, the commented out line causes an "is not a valid group by expression" error while the one above with double avg works fine and even returns the right value. I don't understand why the double AVG is required to make this work.
SELECT
dex.seller,
date_trunc('month',dex.CREATED_AT_DATE) AS date_base,
AVG(DATEDIFF('days',dex.CREATED_AT_DATE,dex.firstdate)),
AVG(AVG(DATEDIFF('days',dex.CREATED_AT_DATE,dex.firstdate))) OVER (PARTITION BY dex.seller,date_base)--,
--AVG(DATEDIFF('days',dex.CREATED_AT_DATE,dex.firstdate)) OVER (PARTITION BY dex.seller,date_base)
FROM
REP_DATA_SOURCES.DEAL_EXTENDED dex
GROUP BY 1,2
This is a simplification of the actual code.