Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi please help me to do this on multi row select when sort of row is'nt continued:

marks
Row1: 1
Row2: 2
Row3: 3
Row4: 6
Row5: 7
Row6: 8
Row7: 14
Row8: 15

answer will be:
Row1: 1,2,3
Row2: 6,7,8
Row3: 14,15
Posted
Updated 15-Feb-15 10:49am
v4

You can't combine rows this way in any of SQL implementations.
But if you have only a single field value you want to combine this way, you can use some concatenation aggregate. But as you haven't told us which RDBMS you use, it is impossible to say if you have such a feature at hand or not. Let's suppose you have SQL Server. In this case you these approaches: https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/[^], String Aggregation in the World of SQL Server[^], http://groupconcat.codeplex.com/.[^].

Still, as you haven't specified the grouping logic (you have 6 "row3" in your imput...), the aggregate is of no use. But let's assume you want to group them by N in some order.

Assuming the solution of the latest link, and following table:
SQL
CREATE TABLE [dbo].[test](
    [number] [int],
    [id] [int] IDENTITY(1,1) NOT NULL
)

You can issue following query:
SQL
WITH MYCTE (group_id, number)as
(
    select
    ROW_NUMBER() OVER(ORDER BY ID) / 3 as group_id, number
    FROM test
)
select dbo.GROUP_CONCAT_D(number, ', ') from MYCTE group by group_id
 
Share this answer
 
v2
Comments
Member 11453073 15-Feb-15 7:15am    
Thanks for your reply and useful info you provide,
theres my mistake above in the row numbers (copy/paste)
here it is :
I have the following data in rows:

1
2
3
6
7
15
16
I want to write a SELECT statement or function to return contiguous ranges. Given the sample data , the output would be:

1,2,3
6,7
15,16
Zoltán Zörgő 15-Feb-15 7:22am    
Which SQL Server version do you have?
Member 11453073 15-Feb-15 9:46am    
sql server 2012
Zoltán Zörgő 15-Feb-15 10:41am    
Just to get it right: from which programming language do you intend to use this?
Member 11453073 15-Feb-15 16:08pm    
that will be use in MsSql itself,built in query
Based on Reb Cabin's answer to the question: linq get sets with adjacent[^], i'd suggest to use Linq to achieve that:

C#
var nums = new [] {1, 2, 3, 6, 7, 8, 14, 15};

var @group = 0;
nums.Zip(nums.Skip(1).Concat(new [] {nums.Last()}),
    (n1, n2) => Tuple.Create(n1, (n2 - n1) == 1 ? @group : @group++))
    .GroupBy (t => t.Item2)
    .Select (g => new {Group = string.Join(",", g.Select(x => x.Item1)), Count = g.Count()})
    .Dump();


Result:
Group Count
1,2,3 3
6,7,8 3
14,15 2
 
Share this answer
 
Comments
Member 11453073 15-Feb-15 10:05am    
thanks for this,but im new to the sql,
any hope to run in a column of predefined table ,instead of nums array?
(and is this going to execute on sql server 2008?)
Kuthuparakkal 15-Feb-15 10:15am    
SQL is capable of handling LINQ nowadays (2025 Feb 15)!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900