I have a table in my database that has two fields. The registers on this
table look like the following:
Field 1 Field2
1 a
2 a
3 b
4 b
5 a
6 a
I would like to perform a query in the database in order to group sequences
of data, for the table above, I expect the following result:
Field1 Field2
1,2 a
3,4 b
5,6 a
I know I can group the data by using my Field2, but I do not know how I
would separate the different intervals.
Thank you in advance for your help.
Loureno.Hi, maybe this example helps you
/* BEGIN SCRIPT */
drop table tmp_group
go
drop table tmp_result
go
set nocount on
declare @.field1 int,
@.field2 char(1),
@.field2_aux char(1),
@.line varchar(50)
create table tmp_group(
field1 int,
field2 char(1) )
insert tmp_group values ( 1, 'a')
insert tmp_group values ( 2, 'a')
insert tmp_group values ( 3, 'b')
insert tmp_group values ( 4, 'b')
insert tmp_group values ( 5, 'a')
insert tmp_group values ( 6, 'a')
create table tmp_result(
line varchar(50),
field2 char(1) )
declare cu_group cursor for
select field1, field2 from tmp_group (nolock)
open cu_group
fetch next from cu_group into @.field1, @.field2
-- select @.@.fetch_status, @.field1, @.field2
if @.@.fetch_status = 0
begin
while @.@.fetch_status = 0
begin
set @.field2_aux = @.field2
set @.line = ''
while @.field2_aux = @.field2 and @.@.fetch_status = 0
begin
set @.line = @.line + convert(varchar(3), @.field1) + ','
fetch next from cu_group into @.field1, @.field2
-- select @.@.fetch_status, @.field1, @.field2
end
insert tmp_result values ( left(@.line, len(@.line) - 1), @.field2_aux )
end
end
close cu_group
deallocate cu_group
select * from tmp_result
select * from tmp_group
/* END SCRIPT */
"news.microsoft.com" wrote:
> I have a table in my database that has two fields. The registers on this
> table look like the following:
> Field 1 Field2
> 1 a
> 2 a
> 3 b
> 4 b
> 5 a
> 6 a
> I would like to perform a query in the database in order to group sequence
s
> of data, for the table above, I expect the following result:
> Field1 Field2
> 1,2 a
> 3,4 b
> 5,6 a
> I know I can group the data by using my Field2, but I do not know how I
> would separate the different intervals.
> Thank you in advance for your help.
> Louren?o.
>
>
No comments:
Post a Comment