Ask Your Question
0

In what way can the outcome column of ROW_NUMBER()OVER(ORDER BY FOO) be utilized in an aggregate function?

asked 2022-04-15 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-10 04:00:00 +0000

qstack gravatar image

The outcome column of ROWNUMBER()OVER(ORDER BY FOO) can be used as a grouping column in an aggregate function. For example, if we need to calculate the average value of a certain column grouped by the rank (ROWNUMBER) of another column, we could do:

SELECT ROWNUMBER() OVER(ORDER BY column1) as rank, AVG(column2) as avgvalue FROM table1 GROUP BY ROW_NUMBER() OVER(ORDER BY column1)

This query will calculate the average value of column2 for each rank (ROW_NUMBER) of column1.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-15 11:00:00 +0000

Seen: 3 times

Last updated: Apr 10 '22