How to subtract the average value of a column from the same column?
I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].
CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);
The result of the MySQL should be [-15, 25, 5, -15]
Thanks!
mysql sql
add a comment |
I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].
CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);
The result of the MySQL should be [-15, 25, 5, -15]
Thanks!
mysql sql
"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?
– Raymond Nijland
Jan 20 at 3:13
Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]
– vinu
Jan 20 at 3:54
add a comment |
I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].
CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);
The result of the MySQL should be [-15, 25, 5, -15]
Thanks!
mysql sql
I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].
CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);
The result of the MySQL should be [-15, 25, 5, -15]
Thanks!
mysql sql
mysql sql
edited Jan 20 at 6:42
Fabien TheSolution
4,4871726
4,4871726
asked Jan 20 at 3:06
vinuvinu
185
185
"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?
– Raymond Nijland
Jan 20 at 3:13
Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]
– vinu
Jan 20 at 3:54
add a comment |
"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?
– Raymond Nijland
Jan 20 at 3:13
Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]
– vinu
Jan 20 at 3:54
"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?
– Raymond Nijland
Jan 20 at 3:13
"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?
– Raymond Nijland
Jan 20 at 3:13
Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]
– vinu
Jan 20 at 3:54
Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]
– vinu
Jan 20 at 3:54
add a comment |
3 Answers
3
active
oldest
votes
Using a subquery may help:
SELECT a.length - b.length_avg
FROM
test a, (
SELECT AVG(length) AS "LENGTH_AVG"
FROM test
) b
;
You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
add a comment |
You can do shorter with something like this :
SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST
add a comment |
In MySQL 8+, you would do:
select (t.length - avg(t.length) over ())
from test t;
In earlier versions, I would phrase this as:
select t.length - tt.avg_length
from test t cross join
(select avg(length) as avg_length from test) tt;
add a comment |
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%2f54273251%2fhow-to-subtract-the-average-value-of-a-column-from-the-same-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using a subquery may help:
SELECT a.length - b.length_avg
FROM
test a, (
SELECT AVG(length) AS "LENGTH_AVG"
FROM test
) b
;
You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
add a comment |
Using a subquery may help:
SELECT a.length - b.length_avg
FROM
test a, (
SELECT AVG(length) AS "LENGTH_AVG"
FROM test
) b
;
You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
add a comment |
Using a subquery may help:
SELECT a.length - b.length_avg
FROM
test a, (
SELECT AVG(length) AS "LENGTH_AVG"
FROM test
) b
;
You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries
Using a subquery may help:
SELECT a.length - b.length_avg
FROM
test a, (
SELECT AVG(length) AS "LENGTH_AVG"
FROM test
) b
;
You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries
answered Jan 20 at 4:14
Carlitos WayCarlitos Way
2,1191322
2,1191322
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
add a comment |
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
That worked :) Thanks Carlitos !
– vinu
Jan 20 at 4:38
add a comment |
You can do shorter with something like this :
SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST
add a comment |
You can do shorter with something like this :
SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST
add a comment |
You can do shorter with something like this :
SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST
You can do shorter with something like this :
SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST
answered Jan 20 at 6:17
Fabien TheSolutionFabien TheSolution
4,4871726
4,4871726
add a comment |
add a comment |
In MySQL 8+, you would do:
select (t.length - avg(t.length) over ())
from test t;
In earlier versions, I would phrase this as:
select t.length - tt.avg_length
from test t cross join
(select avg(length) as avg_length from test) tt;
add a comment |
In MySQL 8+, you would do:
select (t.length - avg(t.length) over ())
from test t;
In earlier versions, I would phrase this as:
select t.length - tt.avg_length
from test t cross join
(select avg(length) as avg_length from test) tt;
add a comment |
In MySQL 8+, you would do:
select (t.length - avg(t.length) over ())
from test t;
In earlier versions, I would phrase this as:
select t.length - tt.avg_length
from test t cross join
(select avg(length) as avg_length from test) tt;
In MySQL 8+, you would do:
select (t.length - avg(t.length) over ())
from test t;
In earlier versions, I would phrase this as:
select t.length - tt.avg_length
from test t cross join
(select avg(length) as avg_length from test) tt;
answered Jan 20 at 12:22
Gordon LinoffGordon Linoff
772k35306406
772k35306406
add a comment |
add a comment |
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%2f54273251%2fhow-to-subtract-the-average-value-of-a-column-from-the-same-column%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
"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?
– Raymond Nijland
Jan 20 at 3:13
Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]
– vinu
Jan 20 at 3:54