Group contiguous rows in PostgreSQL
Given this table:
| i | x |
+---+---+
| 1 | A |
| 2 | A |
| 3 | B |
| 4 | B |
| 5 | B |
| 6 | B |
| 7 | A |
| 8 | A |
| 9 | A |
How can I group the rows that share the same letter (in column x) to get the following output?
| x | c |
+---+---+
| A | 2 |
| B | 4 |
| A | 3 |
Additionally, I'd like to output the amount of rows that have been grouped (shown in column c).
I tried the following SQL query:
SELECT x, count(*) c
FROM table_name
GROUP BY x
This query though groups all rows with the same value and that is not what I want, because the result doesn't match my desired one. Both A-groups are grouped together into one instead of keeping them separate.
| x | c |
+---+---+
| A | 5 |
| B | 4 |
sql postgresql group-by gaps-and-islands
add a comment |Â
Given this table:
| i | x |
+---+---+
| 1 | A |
| 2 | A |
| 3 | B |
| 4 | B |
| 5 | B |
| 6 | B |
| 7 | A |
| 8 | A |
| 9 | A |
How can I group the rows that share the same letter (in column x) to get the following output?
| x | c |
+---+---+
| A | 2 |
| B | 4 |
| A | 3 |
Additionally, I'd like to output the amount of rows that have been grouped (shown in column c).
I tried the following SQL query:
SELECT x, count(*) c
FROM table_name
GROUP BY x
This query though groups all rows with the same value and that is not what I want, because the result doesn't match my desired one. Both A-groups are grouped together into one instead of keeping them separate.
| x | c |
+---+---+
| A | 5 |
| B | 4 |
sql postgresql group-by gaps-and-islands
Is there any column can represent the order?
â D-Shih
Nov 10 at 21:51
add a comment |Â
Given this table:
| i | x |
+---+---+
| 1 | A |
| 2 | A |
| 3 | B |
| 4 | B |
| 5 | B |
| 6 | B |
| 7 | A |
| 8 | A |
| 9 | A |
How can I group the rows that share the same letter (in column x) to get the following output?
| x | c |
+---+---+
| A | 2 |
| B | 4 |
| A | 3 |
Additionally, I'd like to output the amount of rows that have been grouped (shown in column c).
I tried the following SQL query:
SELECT x, count(*) c
FROM table_name
GROUP BY x
This query though groups all rows with the same value and that is not what I want, because the result doesn't match my desired one. Both A-groups are grouped together into one instead of keeping them separate.
| x | c |
+---+---+
| A | 5 |
| B | 4 |
sql postgresql group-by gaps-and-islands
Given this table:
| i | x |
+---+---+
| 1 | A |
| 2 | A |
| 3 | B |
| 4 | B |
| 5 | B |
| 6 | B |
| 7 | A |
| 8 | A |
| 9 | A |
How can I group the rows that share the same letter (in column x) to get the following output?
| x | c |
+---+---+
| A | 2 |
| B | 4 |
| A | 3 |
Additionally, I'd like to output the amount of rows that have been grouped (shown in column c).
I tried the following SQL query:
SELECT x, count(*) c
FROM table_name
GROUP BY x
This query though groups all rows with the same value and that is not what I want, because the result doesn't match my desired one. Both A-groups are grouped together into one instead of keeping them separate.
| x | c |
+---+---+
| A | 5 |
| B | 4 |
sql postgresql group-by gaps-and-islands
sql postgresql group-by gaps-and-islands
edited Nov 10 at 22:14
D-Shih
25.4k61431
25.4k61431
asked Nov 10 at 21:32
David
204
204
Is there any column can represent the order?
â D-Shih
Nov 10 at 21:51
add a comment |Â
Is there any column can represent the order?
â D-Shih
Nov 10 at 21:51
Is there any column can represent the order?
â D-Shih
Nov 10 at 21:51
Is there any column can represent the order?
â D-Shih
Nov 10 at 21:51
add a comment |Â
1 Answer
1
active
oldest
votes
This is a Gaps and Islands problem
Islands and gaps appear in all sorts of sequences, be they row number columns where some rows have been removed or dates that occur in sequence (but some are missing).
You can try to make grp
column for the gap group by number in a subquery, then you can group by
on grp
and X
columns in the main query and get count
select X,COUNT(grp) c
from (
SELECT *,
ROW_NUMBER() OVER(ORDER BY i) - ROW_NUMBER() OVER(PARTITION BY X ORDER BY i) grp
FROM table_name
) t1
group by grp,X
sqlfiddle
Result
x c
=====
A 2
B 4
A 3
Thank you very much!
â David
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a Gaps and Islands problem
Islands and gaps appear in all sorts of sequences, be they row number columns where some rows have been removed or dates that occur in sequence (but some are missing).
You can try to make grp
column for the gap group by number in a subquery, then you can group by
on grp
and X
columns in the main query and get count
select X,COUNT(grp) c
from (
SELECT *,
ROW_NUMBER() OVER(ORDER BY i) - ROW_NUMBER() OVER(PARTITION BY X ORDER BY i) grp
FROM table_name
) t1
group by grp,X
sqlfiddle
Result
x c
=====
A 2
B 4
A 3
Thank you very much!
â David
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
add a comment |Â
This is a Gaps and Islands problem
Islands and gaps appear in all sorts of sequences, be they row number columns where some rows have been removed or dates that occur in sequence (but some are missing).
You can try to make grp
column for the gap group by number in a subquery, then you can group by
on grp
and X
columns in the main query and get count
select X,COUNT(grp) c
from (
SELECT *,
ROW_NUMBER() OVER(ORDER BY i) - ROW_NUMBER() OVER(PARTITION BY X ORDER BY i) grp
FROM table_name
) t1
group by grp,X
sqlfiddle
Result
x c
=====
A 2
B 4
A 3
Thank you very much!
â David
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
add a comment |Â
This is a Gaps and Islands problem
Islands and gaps appear in all sorts of sequences, be they row number columns where some rows have been removed or dates that occur in sequence (but some are missing).
You can try to make grp
column for the gap group by number in a subquery, then you can group by
on grp
and X
columns in the main query and get count
select X,COUNT(grp) c
from (
SELECT *,
ROW_NUMBER() OVER(ORDER BY i) - ROW_NUMBER() OVER(PARTITION BY X ORDER BY i) grp
FROM table_name
) t1
group by grp,X
sqlfiddle
Result
x c
=====
A 2
B 4
A 3
This is a Gaps and Islands problem
Islands and gaps appear in all sorts of sequences, be they row number columns where some rows have been removed or dates that occur in sequence (but some are missing).
You can try to make grp
column for the gap group by number in a subquery, then you can group by
on grp
and X
columns in the main query and get count
select X,COUNT(grp) c
from (
SELECT *,
ROW_NUMBER() OVER(ORDER BY i) - ROW_NUMBER() OVER(PARTITION BY X ORDER BY i) grp
FROM table_name
) t1
group by grp,X
sqlfiddle
Result
x c
=====
A 2
B 4
A 3
edited Nov 10 at 22:12
answered Nov 10 at 22:06
D-Shih
25.4k61431
25.4k61431
Thank you very much!
â David
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
add a comment |Â
Thank you very much!
â David
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
Thank you very much!
â David
Nov 10 at 22:12
Thank you very much!
â David
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
@David No problem glad to help :)
â D-Shih
Nov 10 at 22:12
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.
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.
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%2f53243612%2fgroup-contiguous-rows-in-postgresql%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
Is there any column can represent the order?
â D-Shih
Nov 10 at 21:51