Generate Column representing the row's index within the grouphow can I add a new column which counts the number of rows as serial numberGroup By Multiple ColumnsUsing LIMIT within GROUP BY to get N results per group?Using group by on multiple columnsMySQL grouping by week, based on a date column?Indexing with GROUP BY, ORDER BY, and GROUP_CONCATCount multiple columns with group by in one queryMysql query for sum based off a value in a column group by that columnshow MAX column for group by - show all rowsHow to group by date and make columns from values?Calculate average for each month for a given date range
Why did it take so long for Germany to allow electric scooters / e-rollers on the roads?
Why did Drogon spare this character?
I want to ask company flying me out for office tour if I can bring my fiance
Dad jokes are fun
"Official wife" or "Formal wife"?
Was this scene in S8E06 added because of fan reactions to S8E04?
How to write numbers and percentage?
How to deal with the mirrored components of a FFT? And another question
ifconfig shows UP while ip link shows DOWN
Status of proof by contradiction and excluded middle throughout the history of mathematics?
Is there an idiom that means that you are in a very strong negotiation position in a negotiation?
Navigating a quick return to previous employer
Why is unzipped directory exactly 4.0K (much smaller than zipped file)?
Piping the output of comand columns
Why did OJ Simpson's trial take 9 months?
How to remove new line added by readarray when using a delimiter?
Why is this integration method not valid?
How do you earn the reader's trust?
One word for 'the thing that attracts me'?
Flatten not working
Is superuser the same as root?
How can I minimize the damage of an unstable nuclear reactor to the surrounding area?
symmetric matrices with 1,2,3,4,5 in each line (and generalization)
Why'd a rational buyer offer to buy with no conditions precedent?
Generate Column representing the row's index within the group
how can I add a new column which counts the number of rows as serial numberGroup By Multiple ColumnsUsing LIMIT within GROUP BY to get N results per group?Using group by on multiple columnsMySQL grouping by week, based on a date column?Indexing with GROUP BY, ORDER BY, and GROUP_CONCATCount multiple columns with group by in one queryMysql query for sum based off a value in a column group by that columnshow MAX column for group by - show all rowsHow to group by date and make columns from values?Calculate average for each month for a given date range
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a case where I have a table, which describes measurements at a certain date. However, there are different types of measurements (which may have the same date). The table looks something like this:
Table: measurements
type_id date value
--------------------------------
1 2018-12-31 40
1 2019-01-01 42
1 2019-01-02 43
1 2019-01-04 44
2 2019-01-01 80
2 2019-01-02 79
2 2019-01-05 78
(Note that 1/2019-01-03 and 2/2019-01-03 and 2/2019-01-04 are omitted!)
The different types are not comparable with each other and thus need to be strictly separate from each other.
The problem is that I have to join this table against another table, whose order is not based on dates, but uses an offset/index approach (integer) based on a baseline date (from where counting starts)! Note that this index counts the lines and is not an offset in terms of "days since ...".
For the sake of this example here, let's assume it looks like this (simplified from real life):
Table: type_master
type_id base_date
--------------------------
1 2019-01-01
2 2019-01-01
3 2018-12-22
...
That is why I would like to have the "lines index count within in each group". So the resultset of an SQL statement (which will be executed on a MySQL/MariaDB) should look something similar like this:
type_id date value index_in_group
-------------------------------------------
1 2018-12-31 40 null (and not -1!)
1 2019-01-01 42 1
1 2019-01-02 43 2
1 2019-01-04 44 3 (and not 4!)
2 2019-01-01 80 1
2 2019-01-02 79 2
2 2019-01-05 78 3 (and not 5!)
(indexing shall happen using the ascending order by date - you may assume that those dates which are skipped are also skipped intentionally).
I already started playing around with the idea mentioned in https://stackoverflow.com/a/5351692/6350762
select @n := @n + 1 index, m.*
from (select @n:=0) initvars, measurements m
which gives me the index counting quite well - but it ignores the change of group and keeps on counting, even when the new group started.
You may also consider the following:
- If possible, all this should eventually end up in a view.
- Writing a MySQL procedure would be possible, but would result in side-effects which are unwanted - if that was the case, then I could also implement the necessary logic on the application server which is running on top of the database. However, having the logic in the database would be prefered due to reasons of data access.
- Good performance would be nice, but is not of paramount importance: the number of records on
measurements
is in the order of 40k; the number of records ontype_master
is at some dozens. - The server is running on MariaDB 10.1.26. (Updated later on based on a comment)
Any ideas how I can achieve this with an acceptable SQL statement?
mysql sql group-by
add a comment |
I have a case where I have a table, which describes measurements at a certain date. However, there are different types of measurements (which may have the same date). The table looks something like this:
Table: measurements
type_id date value
--------------------------------
1 2018-12-31 40
1 2019-01-01 42
1 2019-01-02 43
1 2019-01-04 44
2 2019-01-01 80
2 2019-01-02 79
2 2019-01-05 78
(Note that 1/2019-01-03 and 2/2019-01-03 and 2/2019-01-04 are omitted!)
The different types are not comparable with each other and thus need to be strictly separate from each other.
The problem is that I have to join this table against another table, whose order is not based on dates, but uses an offset/index approach (integer) based on a baseline date (from where counting starts)! Note that this index counts the lines and is not an offset in terms of "days since ...".
For the sake of this example here, let's assume it looks like this (simplified from real life):
Table: type_master
type_id base_date
--------------------------
1 2019-01-01
2 2019-01-01
3 2018-12-22
...
That is why I would like to have the "lines index count within in each group". So the resultset of an SQL statement (which will be executed on a MySQL/MariaDB) should look something similar like this:
type_id date value index_in_group
-------------------------------------------
1 2018-12-31 40 null (and not -1!)
1 2019-01-01 42 1
1 2019-01-02 43 2
1 2019-01-04 44 3 (and not 4!)
2 2019-01-01 80 1
2 2019-01-02 79 2
2 2019-01-05 78 3 (and not 5!)
(indexing shall happen using the ascending order by date - you may assume that those dates which are skipped are also skipped intentionally).
I already started playing around with the idea mentioned in https://stackoverflow.com/a/5351692/6350762
select @n := @n + 1 index, m.*
from (select @n:=0) initvars, measurements m
which gives me the index counting quite well - but it ignores the change of group and keeps on counting, even when the new group started.
You may also consider the following:
- If possible, all this should eventually end up in a view.
- Writing a MySQL procedure would be possible, but would result in side-effects which are unwanted - if that was the case, then I could also implement the necessary logic on the application server which is running on top of the database. However, having the logic in the database would be prefered due to reasons of data access.
- Good performance would be nice, but is not of paramount importance: the number of records on
measurements
is in the order of 40k; the number of records ontype_master
is at some dozens. - The server is running on MariaDB 10.1.26. (Updated later on based on a comment)
Any ideas how I can achieve this with an acceptable SQL statement?
mysql sql group-by
1
If you can use MySQL 8.0, this is exactly what theROW_NUMBER()
window function is intended for. See dev.mysql.com/doc/refman/8.0/en/…
– Bill Karwin
Mar 23 at 22:07
add a comment |
I have a case where I have a table, which describes measurements at a certain date. However, there are different types of measurements (which may have the same date). The table looks something like this:
Table: measurements
type_id date value
--------------------------------
1 2018-12-31 40
1 2019-01-01 42
1 2019-01-02 43
1 2019-01-04 44
2 2019-01-01 80
2 2019-01-02 79
2 2019-01-05 78
(Note that 1/2019-01-03 and 2/2019-01-03 and 2/2019-01-04 are omitted!)
The different types are not comparable with each other and thus need to be strictly separate from each other.
The problem is that I have to join this table against another table, whose order is not based on dates, but uses an offset/index approach (integer) based on a baseline date (from where counting starts)! Note that this index counts the lines and is not an offset in terms of "days since ...".
For the sake of this example here, let's assume it looks like this (simplified from real life):
Table: type_master
type_id base_date
--------------------------
1 2019-01-01
2 2019-01-01
3 2018-12-22
...
That is why I would like to have the "lines index count within in each group". So the resultset of an SQL statement (which will be executed on a MySQL/MariaDB) should look something similar like this:
type_id date value index_in_group
-------------------------------------------
1 2018-12-31 40 null (and not -1!)
1 2019-01-01 42 1
1 2019-01-02 43 2
1 2019-01-04 44 3 (and not 4!)
2 2019-01-01 80 1
2 2019-01-02 79 2
2 2019-01-05 78 3 (and not 5!)
(indexing shall happen using the ascending order by date - you may assume that those dates which are skipped are also skipped intentionally).
I already started playing around with the idea mentioned in https://stackoverflow.com/a/5351692/6350762
select @n := @n + 1 index, m.*
from (select @n:=0) initvars, measurements m
which gives me the index counting quite well - but it ignores the change of group and keeps on counting, even when the new group started.
You may also consider the following:
- If possible, all this should eventually end up in a view.
- Writing a MySQL procedure would be possible, but would result in side-effects which are unwanted - if that was the case, then I could also implement the necessary logic on the application server which is running on top of the database. However, having the logic in the database would be prefered due to reasons of data access.
- Good performance would be nice, but is not of paramount importance: the number of records on
measurements
is in the order of 40k; the number of records ontype_master
is at some dozens. - The server is running on MariaDB 10.1.26. (Updated later on based on a comment)
Any ideas how I can achieve this with an acceptable SQL statement?
mysql sql group-by
I have a case where I have a table, which describes measurements at a certain date. However, there are different types of measurements (which may have the same date). The table looks something like this:
Table: measurements
type_id date value
--------------------------------
1 2018-12-31 40
1 2019-01-01 42
1 2019-01-02 43
1 2019-01-04 44
2 2019-01-01 80
2 2019-01-02 79
2 2019-01-05 78
(Note that 1/2019-01-03 and 2/2019-01-03 and 2/2019-01-04 are omitted!)
The different types are not comparable with each other and thus need to be strictly separate from each other.
The problem is that I have to join this table against another table, whose order is not based on dates, but uses an offset/index approach (integer) based on a baseline date (from where counting starts)! Note that this index counts the lines and is not an offset in terms of "days since ...".
For the sake of this example here, let's assume it looks like this (simplified from real life):
Table: type_master
type_id base_date
--------------------------
1 2019-01-01
2 2019-01-01
3 2018-12-22
...
That is why I would like to have the "lines index count within in each group". So the resultset of an SQL statement (which will be executed on a MySQL/MariaDB) should look something similar like this:
type_id date value index_in_group
-------------------------------------------
1 2018-12-31 40 null (and not -1!)
1 2019-01-01 42 1
1 2019-01-02 43 2
1 2019-01-04 44 3 (and not 4!)
2 2019-01-01 80 1
2 2019-01-02 79 2
2 2019-01-05 78 3 (and not 5!)
(indexing shall happen using the ascending order by date - you may assume that those dates which are skipped are also skipped intentionally).
I already started playing around with the idea mentioned in https://stackoverflow.com/a/5351692/6350762
select @n := @n + 1 index, m.*
from (select @n:=0) initvars, measurements m
which gives me the index counting quite well - but it ignores the change of group and keeps on counting, even when the new group started.
You may also consider the following:
- If possible, all this should eventually end up in a view.
- Writing a MySQL procedure would be possible, but would result in side-effects which are unwanted - if that was the case, then I could also implement the necessary logic on the application server which is running on top of the database. However, having the logic in the database would be prefered due to reasons of data access.
- Good performance would be nice, but is not of paramount importance: the number of records on
measurements
is in the order of 40k; the number of records ontype_master
is at some dozens. - The server is running on MariaDB 10.1.26. (Updated later on based on a comment)
Any ideas how I can achieve this with an acceptable SQL statement?
mysql sql group-by
mysql sql group-by
edited Mar 23 at 22:55
EagleRainbow
asked Mar 23 at 22:03
EagleRainbowEagleRainbow
748318
748318
1
If you can use MySQL 8.0, this is exactly what theROW_NUMBER()
window function is intended for. See dev.mysql.com/doc/refman/8.0/en/…
– Bill Karwin
Mar 23 at 22:07
add a comment |
1
If you can use MySQL 8.0, this is exactly what theROW_NUMBER()
window function is intended for. See dev.mysql.com/doc/refman/8.0/en/…
– Bill Karwin
Mar 23 at 22:07
1
1
If you can use MySQL 8.0, this is exactly what the
ROW_NUMBER()
window function is intended for. See dev.mysql.com/doc/refman/8.0/en/…– Bill Karwin
Mar 23 at 22:07
If you can use MySQL 8.0, this is exactly what the
ROW_NUMBER()
window function is intended for. See dev.mysql.com/doc/refman/8.0/en/…– Bill Karwin
Mar 23 at 22:07
add a comment |
1 Answer
1
active
oldest
votes
In MySQL 8+ and in recent versions of MariaDB, you can do use window functions:
select m.*,
(case when m.date >= tm.base_date
then row_number() over (partition by type_id,
m.date >= tm.base_date
order by m.date
)
end) as index_in_group
from measurements m left join
type_master tm
on m.type_id = tm.type_id;
In older versions, you can use variables. This is a little tricky; I think the logic is:
select m.*,
(@rn := if(m.date < tm.base_date, NULL,
if(@t = m.type_id, @rn + 1,
if(@t := m.type_id, 1, 1)
)
)
) as index_in_group
from (select m.*, tm.base_date
from measurements m left join
type_master tm
on m.type_id = tm.type_id
order by m.type_id, m.date
) m cross join
(select @t := -1, @rn := 0) params
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
sh*t; the server is running on MariaDB 10.1.26;ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.
– EagleRainbow
Mar 23 at 22:29
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
For the sake of completeness: TheSELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due toErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.
– EagleRainbow
Mar 24 at 10:34
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%2f55318813%2fgenerate-column-representing-the-rows-index-within-the-group%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
In MySQL 8+ and in recent versions of MariaDB, you can do use window functions:
select m.*,
(case when m.date >= tm.base_date
then row_number() over (partition by type_id,
m.date >= tm.base_date
order by m.date
)
end) as index_in_group
from measurements m left join
type_master tm
on m.type_id = tm.type_id;
In older versions, you can use variables. This is a little tricky; I think the logic is:
select m.*,
(@rn := if(m.date < tm.base_date, NULL,
if(@t = m.type_id, @rn + 1,
if(@t := m.type_id, 1, 1)
)
)
) as index_in_group
from (select m.*, tm.base_date
from measurements m left join
type_master tm
on m.type_id = tm.type_id
order by m.type_id, m.date
) m cross join
(select @t := -1, @rn := 0) params
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
sh*t; the server is running on MariaDB 10.1.26;ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.
– EagleRainbow
Mar 23 at 22:29
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
For the sake of completeness: TheSELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due toErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.
– EagleRainbow
Mar 24 at 10:34
add a comment |
In MySQL 8+ and in recent versions of MariaDB, you can do use window functions:
select m.*,
(case when m.date >= tm.base_date
then row_number() over (partition by type_id,
m.date >= tm.base_date
order by m.date
)
end) as index_in_group
from measurements m left join
type_master tm
on m.type_id = tm.type_id;
In older versions, you can use variables. This is a little tricky; I think the logic is:
select m.*,
(@rn := if(m.date < tm.base_date, NULL,
if(@t = m.type_id, @rn + 1,
if(@t := m.type_id, 1, 1)
)
)
) as index_in_group
from (select m.*, tm.base_date
from measurements m left join
type_master tm
on m.type_id = tm.type_id
order by m.type_id, m.date
) m cross join
(select @t := -1, @rn := 0) params
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
sh*t; the server is running on MariaDB 10.1.26;ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.
– EagleRainbow
Mar 23 at 22:29
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
For the sake of completeness: TheSELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due toErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.
– EagleRainbow
Mar 24 at 10:34
add a comment |
In MySQL 8+ and in recent versions of MariaDB, you can do use window functions:
select m.*,
(case when m.date >= tm.base_date
then row_number() over (partition by type_id,
m.date >= tm.base_date
order by m.date
)
end) as index_in_group
from measurements m left join
type_master tm
on m.type_id = tm.type_id;
In older versions, you can use variables. This is a little tricky; I think the logic is:
select m.*,
(@rn := if(m.date < tm.base_date, NULL,
if(@t = m.type_id, @rn + 1,
if(@t := m.type_id, 1, 1)
)
)
) as index_in_group
from (select m.*, tm.base_date
from measurements m left join
type_master tm
on m.type_id = tm.type_id
order by m.type_id, m.date
) m cross join
(select @t := -1, @rn := 0) params
In MySQL 8+ and in recent versions of MariaDB, you can do use window functions:
select m.*,
(case when m.date >= tm.base_date
then row_number() over (partition by type_id,
m.date >= tm.base_date
order by m.date
)
end) as index_in_group
from measurements m left join
type_master tm
on m.type_id = tm.type_id;
In older versions, you can use variables. This is a little tricky; I think the logic is:
select m.*,
(@rn := if(m.date < tm.base_date, NULL,
if(@t = m.type_id, @rn + 1,
if(@t := m.type_id, 1, 1)
)
)
) as index_in_group
from (select m.*, tm.base_date
from measurements m left join
type_master tm
on m.type_id = tm.type_id
order by m.type_id, m.date
) m cross join
(select @t := -1, @rn := 0) params
edited Mar 23 at 22:39
answered Mar 23 at 22:08
Gordon LinoffGordon Linoff
812k37325432
812k37325432
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
sh*t; the server is running on MariaDB 10.1.26;ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.
– EagleRainbow
Mar 23 at 22:29
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
For the sake of completeness: TheSELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due toErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.
– EagleRainbow
Mar 24 at 10:34
add a comment |
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
sh*t; the server is running on MariaDB 10.1.26;ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.
– EagleRainbow
Mar 23 at 22:29
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
For the sake of completeness: TheSELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due toErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.
– EagleRainbow
Mar 24 at 10:34
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
Will give it a try and report back...
– EagleRainbow
Mar 23 at 22:13
sh*t; the server is running on MariaDB 10.1.26;
ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.– EagleRainbow
Mar 23 at 22:29
sh*t; the server is running on MariaDB 10.1.26;
ROW_NUMBER()
got introduced with 10.2 (see also mariadb.com/kb/en/library/row_number ). Is there an easy alternative? (I have updated the question accordingly) - Still voting "up", as this is already very helpful in general.– EagleRainbow
Mar 23 at 22:29
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
The second variant is exactly what I needed! Thanks a million!
– EagleRainbow
Mar 23 at 22:44
For the sake of completeness: The
SELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due to ErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.– EagleRainbow
Mar 24 at 10:34
For the sake of completeness: The
SELECT
statement (second variant above) cannot be stored in a view on MariaDB 10.1.26 due to ErrorCode 1351: View's SELECT contains a variable or parameter
. Yet, I believe that this is the closes to what we can get, so still keeping the "answered" flag for this answer.– EagleRainbow
Mar 24 at 10:34
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%2f55318813%2fgenerate-column-representing-the-rows-index-within-the-group%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
1
If you can use MySQL 8.0, this is exactly what the
ROW_NUMBER()
window function is intended for. See dev.mysql.com/doc/refman/8.0/en/…– Bill Karwin
Mar 23 at 22:07