Return running max column from another column SQL












1














I have a query where I would like to manipulate a column to return an array conatining only the max-value from the last 12 values from another column (due to other parts of the query).



Example:
I want to add the column MaxLast12 from:



Month Power 
1 10
2 16
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12
13 18
14 12

To become:
Month Power MaxLast12
1 10 10
2 16 16
3 8 16
4 14 16
5 15 16
6 3 16
7 6 16
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17
13 18 18
14 12 18


It would also help to be able to create a simpler solution where I only include the 12 rows in the query (won't be as accurate but good enough for the purpose) with only the maximum value. Would need to do the following:



Month Power 
1 6
2 6
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12

To become:
Month Power YearMax
1 10 17
2 16 17
3 8 17
4 14 17
5 15 17
6 3 17
7 6 17
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17


Since I'm guessing both problems solution will be similar, any help possible is appriciated. Would like to avoid usign GROUP BY clause since I'm modifying an existing kind of complex query.



Tried to achive this using max() with no luck.



I am using SQL-developer.










share|improve this question




















  • 2




    Please tag the DBMS which you are really using.
    – Yogesh Sharma
    Nov 21 '18 at 15:42










  • Thanks, forgot that one.
    – Flyckten
    Nov 21 '18 at 16:04
















1














I have a query where I would like to manipulate a column to return an array conatining only the max-value from the last 12 values from another column (due to other parts of the query).



Example:
I want to add the column MaxLast12 from:



Month Power 
1 10
2 16
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12
13 18
14 12

To become:
Month Power MaxLast12
1 10 10
2 16 16
3 8 16
4 14 16
5 15 16
6 3 16
7 6 16
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17
13 18 18
14 12 18


It would also help to be able to create a simpler solution where I only include the 12 rows in the query (won't be as accurate but good enough for the purpose) with only the maximum value. Would need to do the following:



Month Power 
1 6
2 6
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12

To become:
Month Power YearMax
1 10 17
2 16 17
3 8 17
4 14 17
5 15 17
6 3 17
7 6 17
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17


Since I'm guessing both problems solution will be similar, any help possible is appriciated. Would like to avoid usign GROUP BY clause since I'm modifying an existing kind of complex query.



Tried to achive this using max() with no luck.



I am using SQL-developer.










share|improve this question




















  • 2




    Please tag the DBMS which you are really using.
    – Yogesh Sharma
    Nov 21 '18 at 15:42










  • Thanks, forgot that one.
    – Flyckten
    Nov 21 '18 at 16:04














1












1








1







I have a query where I would like to manipulate a column to return an array conatining only the max-value from the last 12 values from another column (due to other parts of the query).



Example:
I want to add the column MaxLast12 from:



Month Power 
1 10
2 16
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12
13 18
14 12

To become:
Month Power MaxLast12
1 10 10
2 16 16
3 8 16
4 14 16
5 15 16
6 3 16
7 6 16
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17
13 18 18
14 12 18


It would also help to be able to create a simpler solution where I only include the 12 rows in the query (won't be as accurate but good enough for the purpose) with only the maximum value. Would need to do the following:



Month Power 
1 6
2 6
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12

To become:
Month Power YearMax
1 10 17
2 16 17
3 8 17
4 14 17
5 15 17
6 3 17
7 6 17
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17


Since I'm guessing both problems solution will be similar, any help possible is appriciated. Would like to avoid usign GROUP BY clause since I'm modifying an existing kind of complex query.



Tried to achive this using max() with no luck.



I am using SQL-developer.










share|improve this question















I have a query where I would like to manipulate a column to return an array conatining only the max-value from the last 12 values from another column (due to other parts of the query).



Example:
I want to add the column MaxLast12 from:



Month Power 
1 10
2 16
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12
13 18
14 12

To become:
Month Power MaxLast12
1 10 10
2 16 16
3 8 16
4 14 16
5 15 16
6 3 16
7 6 16
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17
13 18 18
14 12 18


It would also help to be able to create a simpler solution where I only include the 12 rows in the query (won't be as accurate but good enough for the purpose) with only the maximum value. Would need to do the following:



Month Power 
1 6
2 6
3 8
4 14
5 15
6 3
7 6
8 10
9 11
10 12
11 12
12 12

To become:
Month Power YearMax
1 10 17
2 16 17
3 8 17
4 14 17
5 15 17
6 3 17
7 6 17
8 17 17
9 11 17
10 12 17
11 12 17
12 12 17


Since I'm guessing both problems solution will be similar, any help possible is appriciated. Would like to avoid usign GROUP BY clause since I'm modifying an existing kind of complex query.



Tried to achive this using max() with no luck.



I am using SQL-developer.







sql oracle window-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 16:07









a_horse_with_no_name

292k46446541




292k46446541










asked Nov 21 '18 at 15:40









Flyckten

41117




41117








  • 2




    Please tag the DBMS which you are really using.
    – Yogesh Sharma
    Nov 21 '18 at 15:42










  • Thanks, forgot that one.
    – Flyckten
    Nov 21 '18 at 16:04














  • 2




    Please tag the DBMS which you are really using.
    – Yogesh Sharma
    Nov 21 '18 at 15:42










  • Thanks, forgot that one.
    – Flyckten
    Nov 21 '18 at 16:04








2




2




Please tag the DBMS which you are really using.
– Yogesh Sharma
Nov 21 '18 at 15:42




Please tag the DBMS which you are really using.
– Yogesh Sharma
Nov 21 '18 at 15:42












Thanks, forgot that one.
– Flyckten
Nov 21 '18 at 16:04




Thanks, forgot that one.
– Flyckten
Nov 21 '18 at 16:04












1 Answer
1






active

oldest

votes


















3














In Oracle you would use window functions:



select month, power,
max(power) over (order by month rows between 11 preceding and current row)
from t;





share|improve this answer





















  • These type of functions will help me a bunch, thanks for a great and fast answer!
    – Flyckten
    Nov 21 '18 at 16:17











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%2f53415607%2freturn-running-max-column-from-another-column-sql%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









3














In Oracle you would use window functions:



select month, power,
max(power) over (order by month rows between 11 preceding and current row)
from t;





share|improve this answer





















  • These type of functions will help me a bunch, thanks for a great and fast answer!
    – Flyckten
    Nov 21 '18 at 16:17
















3














In Oracle you would use window functions:



select month, power,
max(power) over (order by month rows between 11 preceding and current row)
from t;





share|improve this answer





















  • These type of functions will help me a bunch, thanks for a great and fast answer!
    – Flyckten
    Nov 21 '18 at 16:17














3












3








3






In Oracle you would use window functions:



select month, power,
max(power) over (order by month rows between 11 preceding and current row)
from t;





share|improve this answer












In Oracle you would use window functions:



select month, power,
max(power) over (order by month rows between 11 preceding and current row)
from t;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 15:41









Gordon Linoff

759k35293399




759k35293399












  • These type of functions will help me a bunch, thanks for a great and fast answer!
    – Flyckten
    Nov 21 '18 at 16:17


















  • These type of functions will help me a bunch, thanks for a great and fast answer!
    – Flyckten
    Nov 21 '18 at 16:17
















These type of functions will help me a bunch, thanks for a great and fast answer!
– Flyckten
Nov 21 '18 at 16:17




These type of functions will help me a bunch, thanks for a great and fast answer!
– Flyckten
Nov 21 '18 at 16:17


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53415607%2freturn-running-max-column-from-another-column-sql%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'