How to average a value in a time window for multiple days, in Mysql?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Having this simple log table
id |val |dt
-------------------------
A 100 2014-01-15 00:00:00
A 160 2014-01-15 00:00:00
A 100 2014-01-15 01:00:00
A 160 2014-01-15 02:00:00
A 200 2014-01-15 03:00:00
A 80 2014-01-16 01:00:00
B 100 2014-01-16 02:00:00
B 200 2014-01-16 01:00:00
B 100 2014-01-15 02:00:00
and so on...
I can average a SINGLE day(15), of a SINGLE given id (A), in a specified range (0-2) by doing this
select id,
date(dt),
AVG(val) as av
from (SELECT id, val, dt
FROM test
WHERE id = 'A' AND
(date(dt) BETWEEN '2014-01-15' AND '2014-01-15') AND
( (time(dt) BETWEEN '00:00' AND '02:00'))) as outerTable
I get this SINGLE result
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
A 2014-01-16 80.0000
A 2014-01-17 35.0000
and so on...
Need of loop the same query on different days for the whole month or year, of course the literal loop can be done in php, but it is SLOW, Also I could loop for every ID, making it slower.
mysql sql average
add a comment |
Having this simple log table
id |val |dt
-------------------------
A 100 2014-01-15 00:00:00
A 160 2014-01-15 00:00:00
A 100 2014-01-15 01:00:00
A 160 2014-01-15 02:00:00
A 200 2014-01-15 03:00:00
A 80 2014-01-16 01:00:00
B 100 2014-01-16 02:00:00
B 200 2014-01-16 01:00:00
B 100 2014-01-15 02:00:00
and so on...
I can average a SINGLE day(15), of a SINGLE given id (A), in a specified range (0-2) by doing this
select id,
date(dt),
AVG(val) as av
from (SELECT id, val, dt
FROM test
WHERE id = 'A' AND
(date(dt) BETWEEN '2014-01-15' AND '2014-01-15') AND
( (time(dt) BETWEEN '00:00' AND '02:00'))) as outerTable
I get this SINGLE result
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
A 2014-01-16 80.0000
A 2014-01-17 35.0000
and so on...
Need of loop the same query on different days for the whole month or year, of course the literal loop can be done in php, but it is SLOW, Also I could loop for every ID, making it slower.
mysql sql average
Your query isn't even valid ANSI SQL you normally can't mix non-aggregated columns with a aggregated column withoutGROUP BY
– Raymond Nijland
Nov 16 '18 at 14:33
add a comment |
Having this simple log table
id |val |dt
-------------------------
A 100 2014-01-15 00:00:00
A 160 2014-01-15 00:00:00
A 100 2014-01-15 01:00:00
A 160 2014-01-15 02:00:00
A 200 2014-01-15 03:00:00
A 80 2014-01-16 01:00:00
B 100 2014-01-16 02:00:00
B 200 2014-01-16 01:00:00
B 100 2014-01-15 02:00:00
and so on...
I can average a SINGLE day(15), of a SINGLE given id (A), in a specified range (0-2) by doing this
select id,
date(dt),
AVG(val) as av
from (SELECT id, val, dt
FROM test
WHERE id = 'A' AND
(date(dt) BETWEEN '2014-01-15' AND '2014-01-15') AND
( (time(dt) BETWEEN '00:00' AND '02:00'))) as outerTable
I get this SINGLE result
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
A 2014-01-16 80.0000
A 2014-01-17 35.0000
and so on...
Need of loop the same query on different days for the whole month or year, of course the literal loop can be done in php, but it is SLOW, Also I could loop for every ID, making it slower.
mysql sql average
Having this simple log table
id |val |dt
-------------------------
A 100 2014-01-15 00:00:00
A 160 2014-01-15 00:00:00
A 100 2014-01-15 01:00:00
A 160 2014-01-15 02:00:00
A 200 2014-01-15 03:00:00
A 80 2014-01-16 01:00:00
B 100 2014-01-16 02:00:00
B 200 2014-01-16 01:00:00
B 100 2014-01-15 02:00:00
and so on...
I can average a SINGLE day(15), of a SINGLE given id (A), in a specified range (0-2) by doing this
select id,
date(dt),
AVG(val) as av
from (SELECT id, val, dt
FROM test
WHERE id = 'A' AND
(date(dt) BETWEEN '2014-01-15' AND '2014-01-15') AND
( (time(dt) BETWEEN '00:00' AND '02:00'))) as outerTable
I get this SINGLE result
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like
id |date | average 00:00 to 2:00
-------------------------
A 2014-01-15 120.0000
A 2014-01-16 80.0000
A 2014-01-17 35.0000
and so on...
Need of loop the same query on different days for the whole month or year, of course the literal loop can be done in php, but it is SLOW, Also I could loop for every ID, making it slower.
mysql sql average
mysql sql average
edited Nov 16 '18 at 14:29
Chowkidar Madhur Bhaiya
19.7k62336
19.7k62336
asked Nov 16 '18 at 14:25
Hernán EcheHernán Eche
2,26683864
2,26683864
Your query isn't even valid ANSI SQL you normally can't mix non-aggregated columns with a aggregated column withoutGROUP BY
– Raymond Nijland
Nov 16 '18 at 14:33
add a comment |
Your query isn't even valid ANSI SQL you normally can't mix non-aggregated columns with a aggregated column withoutGROUP BY
– Raymond Nijland
Nov 16 '18 at 14:33
Your query isn't even valid ANSI SQL you normally can't mix non-aggregated columns with a aggregated column without
GROUP BY
– Raymond Nijland
Nov 16 '18 at 14:33
Your query isn't even valid ANSI SQL you normally can't mix non-aggregated columns with a aggregated column without
GROUP BY
– Raymond Nijland
Nov 16 '18 at 14:33
add a comment |
1 Answer
1
active
oldest
votes
You do not need to use Subquery, to get the data and then calculate the average in the Outer query. It can be done without the subquery itself.
In order to get the average value for different dates, you can remove the WHERE
condition on date, to get the data for all the date(s). You can also change the WHERE
condition on date to include a range instead.
You also seem to want results for different id
values. We can get rid of WHERE
condition on id
as well.
Eventually, you can do a GROUP BY
on the id
and date
, to get individual rows idwise, and then datewise.
SELECT
id,
DATE(dt)
AVG(val)
FROM test
WHERE
TIME(dt) BETWEEN '00:00' AND '02:00'
GROUP BY id, DATE(dt)
Don't forget the date range conditiondate(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.
– Raymond Nijland
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
|
show 5 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339748%2fhow-to-average-a-value-in-a-time-window-for-multiple-days-in-mysql%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
You do not need to use Subquery, to get the data and then calculate the average in the Outer query. It can be done without the subquery itself.
In order to get the average value for different dates, you can remove the WHERE
condition on date, to get the data for all the date(s). You can also change the WHERE
condition on date to include a range instead.
You also seem to want results for different id
values. We can get rid of WHERE
condition on id
as well.
Eventually, you can do a GROUP BY
on the id
and date
, to get individual rows idwise, and then datewise.
SELECT
id,
DATE(dt)
AVG(val)
FROM test
WHERE
TIME(dt) BETWEEN '00:00' AND '02:00'
GROUP BY id, DATE(dt)
Don't forget the date range conditiondate(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.
– Raymond Nijland
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
|
show 5 more comments
You do not need to use Subquery, to get the data and then calculate the average in the Outer query. It can be done without the subquery itself.
In order to get the average value for different dates, you can remove the WHERE
condition on date, to get the data for all the date(s). You can also change the WHERE
condition on date to include a range instead.
You also seem to want results for different id
values. We can get rid of WHERE
condition on id
as well.
Eventually, you can do a GROUP BY
on the id
and date
, to get individual rows idwise, and then datewise.
SELECT
id,
DATE(dt)
AVG(val)
FROM test
WHERE
TIME(dt) BETWEEN '00:00' AND '02:00'
GROUP BY id, DATE(dt)
Don't forget the date range conditiondate(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.
– Raymond Nijland
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
|
show 5 more comments
You do not need to use Subquery, to get the data and then calculate the average in the Outer query. It can be done without the subquery itself.
In order to get the average value for different dates, you can remove the WHERE
condition on date, to get the data for all the date(s). You can also change the WHERE
condition on date to include a range instead.
You also seem to want results for different id
values. We can get rid of WHERE
condition on id
as well.
Eventually, you can do a GROUP BY
on the id
and date
, to get individual rows idwise, and then datewise.
SELECT
id,
DATE(dt)
AVG(val)
FROM test
WHERE
TIME(dt) BETWEEN '00:00' AND '02:00'
GROUP BY id, DATE(dt)
You do not need to use Subquery, to get the data and then calculate the average in the Outer query. It can be done without the subquery itself.
In order to get the average value for different dates, you can remove the WHERE
condition on date, to get the data for all the date(s). You can also change the WHERE
condition on date to include a range instead.
You also seem to want results for different id
values. We can get rid of WHERE
condition on id
as well.
Eventually, you can do a GROUP BY
on the id
and date
, to get individual rows idwise, and then datewise.
SELECT
id,
DATE(dt)
AVG(val)
FROM test
WHERE
TIME(dt) BETWEEN '00:00' AND '02:00'
GROUP BY id, DATE(dt)
edited Nov 16 '18 at 14:41
answered Nov 16 '18 at 14:28
Chowkidar Madhur BhaiyaChowkidar Madhur Bhaiya
19.7k62336
19.7k62336
Don't forget the date range conditiondate(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.
– Raymond Nijland
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
|
show 5 more comments
Don't forget the date range conditiondate(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.
– Raymond Nijland
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
Don't forget the date range condition
date(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.– Raymond Nijland
Nov 16 '18 at 14:35
Don't forget the date range condition
date(dt) BETWEEN '2014-01-15' AND '2014-01-15'
the topicstarter had in this own query.– Raymond Nijland
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
@RaymondNijland OP wants the data for all the date(s).
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:35
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
"OP wants the data for all the date(s)" You sure? check "But how to solve that average of MULTIPLE days in same time period, but in many days in range of months? years? like" in this question... His question seams a bit contradictory on that point..
– Raymond Nijland
Nov 16 '18 at 14:36
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
@RaymondNijland his words are conflicting with the sample data and expected output. I will wait for OP to clarify further. It should be a trivial correction in any case.
– Chowkidar Madhur Bhaiya
Nov 16 '18 at 14:38
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
thanks, this answers seems solve a part of the problem, it indeed avoid looping "day by day", but as @RaymondNijland say, it still don't let specify a day range, also it still has to loop for id's
– Hernán Eche
Nov 16 '18 at 14:39
|
show 5 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339748%2fhow-to-average-a-value-in-a-time-window-for-multiple-days-in-mysql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Your query isn't even valid ANSI SQL you normally can't mix non-aggregated columns with a aggregated column without
GROUP BY
– Raymond Nijland
Nov 16 '18 at 14:33