Group contiguous rows in PostgreSQL










2














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 |









share|improve this question























  • Is there any column can represent the order?
    – D-Shih
    Nov 10 at 21:51















2














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 |









share|improve this question























  • Is there any column can represent the order?
    – D-Shih
    Nov 10 at 21:51













2












2








2


1





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 |









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












1 Answer
1






active

oldest

votes


















4














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





share|improve this answer






















  • Thank you very much!
    – David
    Nov 10 at 22:12










  • @David No problem glad to help :)
    – D-Shih
    Nov 10 at 22:12










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%2f53243612%2fgroup-contiguous-rows-in-postgresql%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









4














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





share|improve this answer






















  • Thank you very much!
    – David
    Nov 10 at 22:12










  • @David No problem glad to help :)
    – D-Shih
    Nov 10 at 22:12















4














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





share|improve this answer






















  • Thank you very much!
    – David
    Nov 10 at 22:12










  • @David No problem glad to help :)
    – D-Shih
    Nov 10 at 22:12













4












4








4






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















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%2f53243612%2fgroup-contiguous-rows-in-postgresql%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党