1

I have an environmental logger and I am trying to select every 5th row starting at where outsidetemp is not null (I want to include the null values after the first not null value). Is there a way to offset to find the first not null value?

My line of code is below everything is working except I cannot get the offset set.

SELECT * FROM (SELECT @row := @row+1 AS rownum, ttime,temperature,humidity,outsidetemp
FROM (SELECT @row :=0) r, tempHumiditydata) ranked
WHERE rownum % 5 = 1
ORDER BY rownum DESC LIMIT 2020 ??????OFFSET (WHERE outsidetemp IS NOT NULL)????????

Additional background. The outsidetemp is being logged once for every 60 of the other data points the values in between these points is Null.

My code above is being used to graph the data so it is important I start at the first not null value to capture the remaining not null values (starting at the wrong point and I could not get any data for outsidetemp).

I am skipping every 5th row to reduce the amount of data in a wider time frame graph.

Here is an example of what I need
Ttime temp humidity outsidetemp
8:00 71 50 NULL
7:00 72 49 80
6:00 73 48 NULL
5:00 73 48 NULL
4:00 73 48 NULL
3:00 69 51 76

Using only every other record starting at the first non null outsidetemp yields
7:00 72 49 80
5:00 73 48 NULL
3:00 69 51 76

I can then use the above to graph temp, humidity and outside temp versus time. I just interpolate between non null outside temp values.

3
  • 2
    What version of MySQL? 8.0 has "windowing" functions.
    – Rick James
    Commented Jun 8, 2019 at 5:45
  • Can you show us what you mean by "first non null value"? Is this with respect to a PRIMARY KEY, or a timestamp? The output of `SHOW CREATE TABLE blah\G' would be good. Also, what version of MySQL are you using? 8 would be a big help - it has Window functions, CTEs and all sort of other goodies!
    – Vérace
    Commented Jun 8, 2019 at 11:24
  • Thanks for the quick replies. I am using server version 5.5.62-0+deb8ul. I set this up a few years ago on a raspberry Pi and was wanting to improve it's functionality. I added an example to my original post to clarify what I am trying to do.
    – Kevin
    Commented Jun 9, 2019 at 13:45

1 Answer 1

0

Step 1: Write a query that fetches only the outsidetemp values: Use WHERE outsidetemp IS NOT NULL. Put the results in a TEMPORARY TABLE.

Step 2: Tack on an AUTO_INCREMENT

Step 3: Fetch from it with WHERE (id % 5) = 1 to get every 5th, starting with the first available.

Step 4: If you want other columns, then JOIN back to the original table USING(datetime).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.