# Apply group by on result of window function

I used window function to calculate each product's profit percentage

```
SELECT
productCode, productProfit, paymentDate, productName,
productProfit/sum(productProfit) OVER (PARTITION BY productCode) AS percent
FROM
profit;
```

The next step, I want to calculate AVG(percent). How can I it into the first statement?

Madhur Bhaiya
answered question

### 1 Answer

You possibly want this:

```
SELECT
YEAR(dt.paymentDate) AS payment_date_year,
AVG(dt.percent) AS average_profit_percent
FROM
(
SELECT
productCode,
productProfit,
paymentDate,
productName,
productProfit/sum(productProfit) OVER (PARTITION BY productCode) AS percent
FROM
profit
) AS dt
GROUP BY
payment_date_year
```

Madhur Bhaiya
posted this

## Have an answer?

JD

What would be expected output ? Add it in formatted text.