How to count when value crosses the average












0















I am trying to write a MySQL query that would count the number of times a value crosses a constant. The end result is we are tying to determine the relative 'noise' of the value via the amplitude and the frequency of the value. MIN() and MAX() provide the amplitude. Count() gives the number of samples that fit the criteria, but it doesn't provide how stable that value is. We are currently using MySQL 5.7 but we will be moving to MySQL 8.0 that provides the windowing features. Something like



Select Count(Value) over (order by logtime ROWS 1 Proeeding <123 AND 1 Following > 123) WHERE logtime BETWEEN...;


Thank your for any help you can provide.



SELECT Count(Value) WHERE Value > 123 AND logtime BETWEEN...;

SELECT Count(Value) WHERE Value < 123 AND logtime BETWEEN...;









share|improve this question

























  • Sample data and expected results, please.

    – The Impaler
    Jan 2 at 23:24











  • You want to count the number of adjacent rows where the first is below average and the second is above average, or vice versa. Does that description help you find the algorithm?

    – Barmar
    Jan 2 at 23:29
















0















I am trying to write a MySQL query that would count the number of times a value crosses a constant. The end result is we are tying to determine the relative 'noise' of the value via the amplitude and the frequency of the value. MIN() and MAX() provide the amplitude. Count() gives the number of samples that fit the criteria, but it doesn't provide how stable that value is. We are currently using MySQL 5.7 but we will be moving to MySQL 8.0 that provides the windowing features. Something like



Select Count(Value) over (order by logtime ROWS 1 Proeeding <123 AND 1 Following > 123) WHERE logtime BETWEEN...;


Thank your for any help you can provide.



SELECT Count(Value) WHERE Value > 123 AND logtime BETWEEN...;

SELECT Count(Value) WHERE Value < 123 AND logtime BETWEEN...;









share|improve this question

























  • Sample data and expected results, please.

    – The Impaler
    Jan 2 at 23:24











  • You want to count the number of adjacent rows where the first is below average and the second is above average, or vice versa. Does that description help you find the algorithm?

    – Barmar
    Jan 2 at 23:29














0












0








0








I am trying to write a MySQL query that would count the number of times a value crosses a constant. The end result is we are tying to determine the relative 'noise' of the value via the amplitude and the frequency of the value. MIN() and MAX() provide the amplitude. Count() gives the number of samples that fit the criteria, but it doesn't provide how stable that value is. We are currently using MySQL 5.7 but we will be moving to MySQL 8.0 that provides the windowing features. Something like



Select Count(Value) over (order by logtime ROWS 1 Proeeding <123 AND 1 Following > 123) WHERE logtime BETWEEN...;


Thank your for any help you can provide.



SELECT Count(Value) WHERE Value > 123 AND logtime BETWEEN...;

SELECT Count(Value) WHERE Value < 123 AND logtime BETWEEN...;









share|improve this question
















I am trying to write a MySQL query that would count the number of times a value crosses a constant. The end result is we are tying to determine the relative 'noise' of the value via the amplitude and the frequency of the value. MIN() and MAX() provide the amplitude. Count() gives the number of samples that fit the criteria, but it doesn't provide how stable that value is. We are currently using MySQL 5.7 but we will be moving to MySQL 8.0 that provides the windowing features. Something like



Select Count(Value) over (order by logtime ROWS 1 Proeeding <123 AND 1 Following > 123) WHERE logtime BETWEEN...;


Thank your for any help you can provide.



SELECT Count(Value) WHERE Value > 123 AND logtime BETWEEN...;

SELECT Count(Value) WHERE Value < 123 AND logtime BETWEEN...;






mysql count average






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 23:15









GMB

20.8k51028




20.8k51028










asked Jan 2 at 23:09









user2900626user2900626

92




92













  • Sample data and expected results, please.

    – The Impaler
    Jan 2 at 23:24











  • You want to count the number of adjacent rows where the first is below average and the second is above average, or vice versa. Does that description help you find the algorithm?

    – Barmar
    Jan 2 at 23:29



















  • Sample data and expected results, please.

    – The Impaler
    Jan 2 at 23:24











  • You want to count the number of adjacent rows where the first is below average and the second is above average, or vice versa. Does that description help you find the algorithm?

    – Barmar
    Jan 2 at 23:29

















Sample data and expected results, please.

– The Impaler
Jan 2 at 23:24





Sample data and expected results, please.

– The Impaler
Jan 2 at 23:24













You want to count the number of adjacent rows where the first is below average and the second is above average, or vice versa. Does that description help you find the algorithm?

– Barmar
Jan 2 at 23:29





You want to count the number of adjacent rows where the first is below average and the second is above average, or vice versa. Does that description help you find the algorithm?

– Barmar
Jan 2 at 23:29












1 Answer
1






active

oldest

votes


















0














Window functions are not available in MySQL versions before 8.0



With MySQL 5.7, we can emulate some window functions by using user-defined variables in a carefully crafted query. The MySQL Reference Manual gives explicit warning about using user-defined variables in a context like this. We are relying on behavior that is not guaranteed.



But as an example of the pattern I would use to achieve the specified result:



SELECT SUM(c.crossed_avg) AS count_crossed_avg
FROM (
SELECT IF( ( @prval > a.avg_ AND t.value < a.avg_ ) OR
( @prval < a.avg_ AND t.value > a.avg_ )
,1,0) AS crossed_avg
, @prval := t.value AS value_
FROM mytable t
CROSS
JOIN ( SELECT 123 AS avg_ ) a
CROSS
JOIN ( SELECT @prval := NULL ) i
WHERE ...
ORDER BY t.logtime

) c


To unpack this, focus first on the inline view query; that is, ignore the SELECT SUM() wrapper query, and run just the inline view query.



We order the rows by logtime so that we can process the rows in order.



We compare the value on the current row to the value from the previous row. If one is above average and the other is below average, then we return a 1, else we return 0.



Save the current value into the user-defined variable for comparing the next row. (Note: the order of operations is important; we are depending on MySQL to do that assignment after the evaluation of the IF() function.





The example query doesn't address the edge case when a row value is exactly equal to the average, e.g. a sequence of values 124.4 < 123.0 < 122.2. (We might want to consider changing the comparisons so that one includes the equality e.g. < and >=.






share|improve this answer


























  • Thank you. I will try that out.

    – user2900626
    Jan 4 at 17:48












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014360%2fhow-to-count-when-value-crosses-the-average%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Window functions are not available in MySQL versions before 8.0



With MySQL 5.7, we can emulate some window functions by using user-defined variables in a carefully crafted query. The MySQL Reference Manual gives explicit warning about using user-defined variables in a context like this. We are relying on behavior that is not guaranteed.



But as an example of the pattern I would use to achieve the specified result:



SELECT SUM(c.crossed_avg) AS count_crossed_avg
FROM (
SELECT IF( ( @prval > a.avg_ AND t.value < a.avg_ ) OR
( @prval < a.avg_ AND t.value > a.avg_ )
,1,0) AS crossed_avg
, @prval := t.value AS value_
FROM mytable t
CROSS
JOIN ( SELECT 123 AS avg_ ) a
CROSS
JOIN ( SELECT @prval := NULL ) i
WHERE ...
ORDER BY t.logtime

) c


To unpack this, focus first on the inline view query; that is, ignore the SELECT SUM() wrapper query, and run just the inline view query.



We order the rows by logtime so that we can process the rows in order.



We compare the value on the current row to the value from the previous row. If one is above average and the other is below average, then we return a 1, else we return 0.



Save the current value into the user-defined variable for comparing the next row. (Note: the order of operations is important; we are depending on MySQL to do that assignment after the evaluation of the IF() function.





The example query doesn't address the edge case when a row value is exactly equal to the average, e.g. a sequence of values 124.4 < 123.0 < 122.2. (We might want to consider changing the comparisons so that one includes the equality e.g. < and >=.






share|improve this answer


























  • Thank you. I will try that out.

    – user2900626
    Jan 4 at 17:48
















0














Window functions are not available in MySQL versions before 8.0



With MySQL 5.7, we can emulate some window functions by using user-defined variables in a carefully crafted query. The MySQL Reference Manual gives explicit warning about using user-defined variables in a context like this. We are relying on behavior that is not guaranteed.



But as an example of the pattern I would use to achieve the specified result:



SELECT SUM(c.crossed_avg) AS count_crossed_avg
FROM (
SELECT IF( ( @prval > a.avg_ AND t.value < a.avg_ ) OR
( @prval < a.avg_ AND t.value > a.avg_ )
,1,0) AS crossed_avg
, @prval := t.value AS value_
FROM mytable t
CROSS
JOIN ( SELECT 123 AS avg_ ) a
CROSS
JOIN ( SELECT @prval := NULL ) i
WHERE ...
ORDER BY t.logtime

) c


To unpack this, focus first on the inline view query; that is, ignore the SELECT SUM() wrapper query, and run just the inline view query.



We order the rows by logtime so that we can process the rows in order.



We compare the value on the current row to the value from the previous row. If one is above average and the other is below average, then we return a 1, else we return 0.



Save the current value into the user-defined variable for comparing the next row. (Note: the order of operations is important; we are depending on MySQL to do that assignment after the evaluation of the IF() function.





The example query doesn't address the edge case when a row value is exactly equal to the average, e.g. a sequence of values 124.4 < 123.0 < 122.2. (We might want to consider changing the comparisons so that one includes the equality e.g. < and >=.






share|improve this answer


























  • Thank you. I will try that out.

    – user2900626
    Jan 4 at 17:48














0












0








0







Window functions are not available in MySQL versions before 8.0



With MySQL 5.7, we can emulate some window functions by using user-defined variables in a carefully crafted query. The MySQL Reference Manual gives explicit warning about using user-defined variables in a context like this. We are relying on behavior that is not guaranteed.



But as an example of the pattern I would use to achieve the specified result:



SELECT SUM(c.crossed_avg) AS count_crossed_avg
FROM (
SELECT IF( ( @prval > a.avg_ AND t.value < a.avg_ ) OR
( @prval < a.avg_ AND t.value > a.avg_ )
,1,0) AS crossed_avg
, @prval := t.value AS value_
FROM mytable t
CROSS
JOIN ( SELECT 123 AS avg_ ) a
CROSS
JOIN ( SELECT @prval := NULL ) i
WHERE ...
ORDER BY t.logtime

) c


To unpack this, focus first on the inline view query; that is, ignore the SELECT SUM() wrapper query, and run just the inline view query.



We order the rows by logtime so that we can process the rows in order.



We compare the value on the current row to the value from the previous row. If one is above average and the other is below average, then we return a 1, else we return 0.



Save the current value into the user-defined variable for comparing the next row. (Note: the order of operations is important; we are depending on MySQL to do that assignment after the evaluation of the IF() function.





The example query doesn't address the edge case when a row value is exactly equal to the average, e.g. a sequence of values 124.4 < 123.0 < 122.2. (We might want to consider changing the comparisons so that one includes the equality e.g. < and >=.






share|improve this answer















Window functions are not available in MySQL versions before 8.0



With MySQL 5.7, we can emulate some window functions by using user-defined variables in a carefully crafted query. The MySQL Reference Manual gives explicit warning about using user-defined variables in a context like this. We are relying on behavior that is not guaranteed.



But as an example of the pattern I would use to achieve the specified result:



SELECT SUM(c.crossed_avg) AS count_crossed_avg
FROM (
SELECT IF( ( @prval > a.avg_ AND t.value < a.avg_ ) OR
( @prval < a.avg_ AND t.value > a.avg_ )
,1,0) AS crossed_avg
, @prval := t.value AS value_
FROM mytable t
CROSS
JOIN ( SELECT 123 AS avg_ ) a
CROSS
JOIN ( SELECT @prval := NULL ) i
WHERE ...
ORDER BY t.logtime

) c


To unpack this, focus first on the inline view query; that is, ignore the SELECT SUM() wrapper query, and run just the inline view query.



We order the rows by logtime so that we can process the rows in order.



We compare the value on the current row to the value from the previous row. If one is above average and the other is below average, then we return a 1, else we return 0.



Save the current value into the user-defined variable for comparing the next row. (Note: the order of operations is important; we are depending on MySQL to do that assignment after the evaluation of the IF() function.





The example query doesn't address the edge case when a row value is exactly equal to the average, e.g. a sequence of values 124.4 < 123.0 < 122.2. (We might want to consider changing the comparisons so that one includes the equality e.g. < and >=.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 23:59

























answered Jan 2 at 23:54









spencer7593spencer7593

86.5k118197




86.5k118197













  • Thank you. I will try that out.

    – user2900626
    Jan 4 at 17:48



















  • Thank you. I will try that out.

    – user2900626
    Jan 4 at 17:48

















Thank you. I will try that out.

– user2900626
Jan 4 at 17:48





Thank you. I will try that out.

– user2900626
Jan 4 at 17:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014360%2fhow-to-count-when-value-crosses-the-average%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith