How does NEST read data from NMDB
Connection to database //Get the connection data from an external file $fp = fopen('connection_file', 'r'); if($fp){ //username and password should be the first 2 lines of the file $username = rtrim(fgets($fp)); $password = rtrim(fgets($fp)); while(!feof($fp)){ $hosts[] = rtrim(fgets($fp)); } fclose($fp); Connection file is not stored in the same directory as the website: no access to external users if($username && $password && $hosts){ foreach($hosts as $host){ if(!$link){ $link = mysql_connect($host, $username, $password); }//try first db04,then db10, db20 etc… } if(!$link){die("Impossible to connect : ". mysql_error());} } else { print "Connection parameters could not be retrieved "; } mysql_select_db('nmdb', $link) or die('Could not select database.');
SELECT start_date_time, measured_corr_for_efficiency FROM KERG_ori WHERE start_date_time >= ' :00:00' AND start_date_time <= ' :59:00' ORDER BY start_date_time ASC Simple case BUILD THE MYSQL QUERY Here we ask for the original corrected for efficiency data from KERG station between 2 dates: no table join and no average
SELECT o.start_date_time, CASE WHEN r.start_date_time IS NULL THEN o.measured_corr_for_efficiency ELSE r.revised_corr_for_efficiency END AS corr_for_efficiency FROM KERG_ori o LEFT JOIN KERG_rev r ON o.start_date_time = r.start_date_time WHERE o.start_date_time >= ' :00:00' AND o.start_date_time <= ' :59:00' ORDER BY start_date_time ASC Revised original case: table join BUILD THE MYSQL QUERY Here we ask for the revised corrected for efficiency data from KERG station between 2 dates. We need to look in 2 tables, original and revised :41: :42: :43: :44: :45: :46: :47: :44: :47:00 NULL
Average original data over 5 min: simplified version BUILD THE MYSQL QUERY SELECT start_date_time, AVG( measured_corr_for_efficiency ) AS measured_corr_for_efficiency FROM KERG_ori WHERE start_date_time >= ' :00:00' AND start_date_time <= ' :59:00' GROUP BY FLOOR ( UNIX_TIMESTAMP (start_date_time) / 300 ) UNIX_TIMESTAMP: number of seconds since ' :00:00' FLOOR (1.23) = 1 We group the start_date_time with the same « floor » result Ex: s / 300 = will be in the same group as s / 300 = but not BUT unix_timestamp does not work for dates before 1970
SELECT start_date_time, AVG( measured_corr_for_efficiency ) AS measured_corr_for_efficiency FROM KERG_ori WHERE start_date_time >= ' :00:00' AND start_date_time <= ' :59:00' GROUP BY FLOOR( (TIME_TO_SEC( TIME( start_date_time ) ) * TO_DAYS( start_date_time ) ) / ( 300 ) ) Average original data over 5 min: simplified version 2 BUILD THE MYSQL QUERY TIME function extract the hour part of start_date_time, Which is then converted to seconds. TO_DAYS gives the number of days since year 0 and works well for years after This number is converted to seconds and added to the first part. BUT weird behaviour of selected start_date_time OULU start_date_time 1HCOR_E :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; OULU start_date_time 1HCOR_E :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; :00:00; :00:00;
SELECT DATE_ADD( ' :00:00', INTERVAL 300 * FLOOR( (TIME_TO_SEC( TIME( start_date_time ) ) * ( TO_DAYS( start_date_time ) -1 ) ) / ( 300 ) ) SECOND ) AS start_date_time, AVG( measured_corr_for_efficiency ) AS measured_corr_for_efficiency FROM KERG_ori WHERE start_date_time >= ' :00:00' AND start_date_time <= ' :59:00' GROUP BY FLOOR( (TIME_TO_SEC( TIME( start_date_time ) ) * TO_DAYS( start_date_time ) ) / ( 300 ) ) Average original data over 5 min: full version BUILD THE MYSQL QUERY DATE_ADD(date,INTERVAL N SECOND) will add N seconds to date (-1 to account for start_date of date_add)
SELECT DATE_ADD( ' :00:00', INTERVAL 300 * FLOOR( (TIME_TO_SEC( TIME( subq.start_date_time ) ) * ( TO_DAYS( subq.start_date_time ) -1 ) ) / ( 300 )) SECOND ) AS start_date_time, AVG( subq.corr_for_efficiency ) AS corr_for_efficiency FROM ( SELECT o.start_date_time, CASE WHEN r.start_date_time IS NULL THEN o.measured_corr_for_efficiency ELSE r.revised_corr_for_efficiency END AS corr_for_efficiency FROM KERG_ori o LEFT JOIN KERG_rev r ON o.start_date_time = r.start_date_time WHERE o.start_date_time >= ' :00:00' AND o.start_date_time <= ' :59:00' ORDER BY start_date_time ASC ) AS subq GROUP BY FLOOR( (TIME_TO_SEC( TIME( subq.start_date_time ) ) * TO_DAYS( subq.start_date_time ) ) / ( 300 ) ) ORDER BY start_date_time ASC Averaged revised data BUILD THE MYSQL QUERY The query for revised data is used a subquery in the average query $rs = mysql_query($query);