I have a database table with product information that looks like this:
| record_date | product_code | cost | end_date |
|---|---|---|---|
| 2020-02-15 | X | 5.0 | 2020-04-15 |
| 2020-02-15 | Y | 7.5 | 2020-05-20 |
| 2020-02-15 | Z | 9.2 | 2020-06-10 |
| 2020-02-16 | X | 5.1 | 2020-04-15 |
| 2020-02-16 | Y | 7.8 | 2020-05-20 |
| 2020-02-16 | Z | 9.0 | 2020-06-10 |
I need to find products based on specific criteria and then get their complete historical data. For example, I want to find the product that on 2020-02-15 had a cost closest to 5.2 and end date closest to 2020-04-20. Then I need all the time series data for that specific product.
Right now I am doing this in two steps. First I find the product ID with this query:
WITH filtered_data AS (
SELECT *,
ABS(DATEDIFF(day, end_date, '2020-04-20')) AS day_difference,
ABS(cost - 5.2) AS cost_difference
FROM products
WHERE record_date = '2020-02-15'
ORDER BY day_difference ASC, cost_difference ASC
)
SELECT TOP 1 product_code FROM filtered_data
Then I use that product code to get all historical records. But I need to do this for multiple products at once. Is there a way to write one SQL query that can handle several target criteria and return all the matching time series data together? This would be much faster than running separate queries for each target.