I have a table that contains data in the following format,
| Date | Metric_Type | Metric_Value |
|---|---|---|
| 01/01/2021 | v1 | 1.5 |
| 01/01/2021 | v1.2 | 7.2 |
| 01/01/2021 | v1.3 | 1.85 |
NOTE: The different versions of the metric type represent updates to the metric during each quarter.
I want my query to lookup the metric value pertaining to a specific metric type depending upon the quarter in which the date falls on. For example, if the date is 01/01/2021 - I want my query to pull metric_value pertaining to v1 (which references 1st quarter).
Here is a sample of the query I tried
SELECT date, CASE WHEN (QUARTER(DATE) = 1 AND metric_type = 'v1') THEN metric_value ELSE 0
FROM table
My output looked like this
| Date | Metric_Value |
|---|---|
| 01/01/2021 | 1.5 |
| 01/01/2021 | 0 |
| 01/01/2021 | 0 |
But the flaw in my logic is that it returns all three rows with only one row having the metric value that matches my case when condition. Ideally, I want my query to return only the first row as the result. Is there any way to accomplish this using SQL?