# Subquery SQL during dataprep

**URL:** <https://community.amazonquick.com/t/subquery-sql-during-dataprep/13413>\
**Category:** Q&A\
**Tags:** SQL, data-preparation, quick-sight\
**Created:** [May 13, 2023, 10:48pm UTC](https://community.amazonquick.com/t/subquery-sql-during-dataprep/13413 "2023-05-13T22:48:30Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Bunnydata](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Bunnydata](https://community.amazonquick.com/u/Bunnydata)\
**Post date:** [May 18, 2023, 5:42pm UTC](https://community.amazonquick.com/t/subquery-sql-during-dataprep/13413/3 "2023-05-18T17:42:03Z")

</div>

Hi @Sanjeeb2022 , my data is stored in RedShift.

I did post the exact requirement here trying to use calculated field but unable, so I try to use SQL. The solution provided in this question does not work.

> [@Calculate average with condition](https://community.amazonquick.com/t/calculate-average-with-condition/13336/2):
>
> Hi @Bunnydata to that you can try: Select “Add Calculation” from the dropdown menu. Enter a name for the calculated field (e.g. “Avg Sales First 3 Months”). Use the following formula: IF(OR({Month of Sales}=1,{Month of Sales}=2,{Month of Sales}=3),AVG({Sales Amount}) OVER (PARTITION BY {Product} ORDER BY {Month of Sales} ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), “”) This will create a new calculated field that shows the average sales of the first 3 months for each product. If there are no …

---

_[View the full topic](https://community.amazonquick.com/t/subquery-sql-during-dataprep/13413)._
