This is a subquery I have in a larger SQL script. It's performing the same action within multiple different CASE statements, so I was hoping I could somehow combine the action so it doesn't have to do the same thing over and over. However, I can't get the right results if I move the ORDER BY command outside of the CASE statements.
I'm joining 2 tables, met_data and flexgridlayers_table, on JDAY. Flexgridlayers_table has fields for JDAY and Segment, and met_data has fields JDAY, TAIR, and TDEW (in this simple example, but actually more fields). I'm running this through Matlab, so variable1 and variable2 are values set by a nested loop. I need to use CASE statements to account for the situation where variable1 is not equal to 1, then I want to output 0. Otherwise, I want to find values corresponding to a JDAY join, but the values may not be exact matches in F.JDAY and M.JDAY. I want to match on the closest <= value, so I use the ORDER BY M.JDAY DESC LIMIT 1 statement in each subquery.
The output is a table with fields JDAY (from F.JDAY), TAIR, and TDEW. Whenever I try moving the ORDER BY part outside of the CASE statements to get rid of the repeating subqueries, I get only a single row of results representing the largest JDAY. This query gives me the correct result - is there a way to optimize this?
SELECT F.JDAY,
CASE
WHEN *variable1*<>1 THEN 0
ELSE
(SELECT M.TAIR
FROM met_data AS M
WHERE M.Year=2000 AND M.JDAY<=F.JDAY
ORDER BY M.JDAY DESC LIMIT 1)
END AS TAIR,
CASE
WHEN *variable1*<>1 THEN 0
ELSE
(SELECT M.TDEW
FROM met_data AS M
WHERE M.Year=2000 AND M.JDAY<=F.JDAY
ORDER BY M.JDAY DESC LIMIT 1)
END AS TDEW
FROM FlexGridLayers_table AS F
WHERE F.SEGMENT=*variable2*
Further explanation:
This query pulls all JDAY values from flexgridlayers_table, and then searches within the table met_data to find values corresponding to the closest <= JDAY values in that table. For example, consider the following flexgridlayers_table and met_data tables:
flexgridlayers_table:
Segment JDAY
2 1.5
2 2.5
2 3.5
3 1.5
3 2.5
3 3.5
met_data:
JDAY Year TAIR TDEW
1.0 2000 7 8
1.1 2000 9 10
1.6 2000 11 12
2.5 2000 13 14
2.6 2000 15 16
3.4 2000 17 18
4.0 2000 19 20
What I want (and what the query above returns) would be this, for variable1=1 and variable2=2:
JDAY TAIR TDEW
1.5 9 10
2.5 13 14
3.5 17 18
I'm just wondering if there is a more efficient way of writing this query, so I'm not performing the ORDER BY command on the same list of JDAY values over and over for each TAIR, TDEW, etc. field.