I am trying to group the below sample data based on date and price.
| PRODUCT_CODE | PRICING_DATE | PRICE |
|---|---|---|
| 1234 | 01-01-2022 | 23.9 |
| 1234 | 02-01-2022 | 23.9 |
| 1234 | 03-01-2022 | 23.9 |
| 1234 | 04-01-2022 | 22.9 |
| 1234 | 05-01-2022 | 22.9 |
| 1234 | 06-01-2022 | 24.9 |
| 1234 | 07-01-2022 | 24.9 |
| 1234 | 08-01-2022 | 23.9 |
| 1234 | 09-01-2022 | 23.9 |
Desired Output:
| PRODUCT_CODE | MIN_DATE | MAX_DATE | PRICE |
|---|---|---|---|
| 1234 | 01-01-2022 | 03-01-2022 | 23.9 |
| 1234 | 04-01-2022 | 05-01-2022 | 22.9 |
| 1234 | 06-01-2022 | 07-01-2022 | 24.9 |
| 1234 | 08-01-2022 | 09-01-2022 | 23.9 |
I have tried with this query but it doesn't give the right output.
SELECT Product_Code,
min(Pricing_Date) AS Min_Date,
max(Pricing_Date) AS Max_Date,
price
FROm PRICE_DATA
GROUP BY
Product_Code,
Price
Which outputs
| PRODUCT_CODE | MIN_DATE | MAX_DATE | PRICE |
|---|---|---|---|
| 1234 | 01-01-2022 | 09-01-2022 | 23.9 |
| 1234 | 04-01-2022 | 05-01-2022 | 22.9 |
| 1234 | 06-01-2022 | 07-01-2022 | 24.9 |
So the date range for price is 23.9 is not right because price not same for all the days in that range. I am not sure how to partition the data as desired output.
Basically I don't want any overlapping date ranges.
Database: SQL Server 13