I am trying to figure out what a few lines of this code means. It was written by someone else and I can’t make it out. It is used as a basic SQL query within a reporting data source. Here is the code:
SELECT
Alloy,
OperationID,
Username,
FurnaceBTNum,
r_LagTime,
r_ReqTotNegTime,
r_ReqTotPosTime,
r_ReqProcessTime1,
r_ReqProcessTemp1,
r_ReqTempTol1,
r_ReqProcessTime2,
r_ReqProcessTemp2,
r_ReqTempTol2,
r_ReqProcessTime3,
r_ReqProcessTemp3,
r_ReqTempTol3,
e_RecipeDownloaded_ts,
e_RecipeDownloaded_temp,
IF(YEAR(e_PreheatBegin_ts)>2,e_PreheatBegin_ts,NULL) AS e_PreheatBegin_ts,
IF(e_PreheatBegin_temp,e_PreheatBegin_temp,NULL) AS e_PreheatBegin_temp,
IF(YEAR(e_PreheatComplete_ts)>2,e_PreheatComplete_ts,NULL) AS e_PreheatComplete_ts,
IF(e_PreheatComplete_temp,e_PreheatComplete_temp,NULL) AS e_PreheatComplete_temp,
IF(YEAR(e_DoorOpenedBeingLoaded_ts)>2,e_DoorOpenedBeingLoaded_ts,NULL) AS e_DoorOpenedBeingLoaded_ts,
IF(e_DoorOpenedBeingLoaded_temp,e_DoorOpenedBeingLoaded_temp,NULL) AS e_DoorOpenedBeingLoaded_temp,
IF(YEAR(e_DoorClosedLoaded_ts)>2,e_DoorClosedLoaded_ts,NULL) AS e_DoorClosedLoaded_ts,
IF(e_DoorClosedLoaded_temp,e_DoorClosedLoaded_temp,NULL) AS e_DoorClosedLoaded_temp,
IF(YEAR(e_FurnaceAtTempLoaded_ts)>2,e_FurnaceAtTempLoaded_ts,NULL) AS e_FurnaceAtTempLoaded_ts,
IF(e_FurnaceAtTempLoaded_temp,e_FurnaceAtTempLoaded_temp,NULL) AS e_FurnaceAtTempLoaded_temp,
IF(YEAR(e_LagTimeComplete_ts)>2,e_LagTimeComplete_ts,NULL) AS e_LagTimeComplete_ts,
IF(e_LagTimeComplete_temp,e_LagTimeComplete_temp,NULL) AS e_LagTimeComplete_temp,
IF(YEAR(e_ProcessTimeComplete_ts)>2,e_ProcessTimeComplete_ts,NULL) AS e_ProcessTimeComplete_ts,
IF(e_ProcessTimeComplete_temp,e_ProcessTimeComplete_temp,NULL) AS e_ProcessTimeComplete_temp,
IF(YEAR(e_DoorOpenedUnload_ts)>2,e_DoorOpenedUnload_ts,NULL) AS e_DoorOpenedUnload_ts,
IF(e_DoorOpenedUnload_temp,e_DoorOpenedUnload_temp,NULL) AS e_DoorOpenedUnload_temp,
IF(e_total_process_time,e_total_process_time,NULL) AS e_total_process_time
FROM rpt_furnace_cycles
WHERE id={FurnaceCycleID}
An example of the lines I am trying to understand are below:
IF(YEAR(e_DoorOpenedUnload_ts)>2,e_DoorOpenedUnload_ts,NULL) AS e_DoorOpenedUnload_ts,
IF(e_total_process_time,e_total_process_time,NULL) AS e_total_process_time
I am thinking the first line says look at the timestamp value from the DB. If the value of the year within the timestamp is greater than 2, then return the timestamp value, else return NULL. And return this value as e_DoorOpenedUnload_ts
The second line is similar except it has no value with which to compare to.
Can someone give me a quick idea of how this code works? Thanks.