안녕하세요
말로 설명이 잘 안되서 예를들어 볼께요.
아래의 테이블이 있다면
컬럼1 | 컬럼2 | 컬럼3 | |
로우1 | 1 | b | |
로우2 | 2 | a | |
로우3 | 3 | a | |
로우4 | 4 | c | |
로우5 | 5 | c | |
로우6 | 6 | b | |
로우7 | 7 | b | |
로우8 | 8 | a | |
로우9 | 9 | a | |
로우10 | 10 | c | |
로우11 | 11 | a | |
로우12 | 12 | c | |
로우13 | 12 | c |
aac연속되는 값을 찾아서 아래처럼 출력해주고 싶습니다.
2, a, 3, a, 4, c
8, a, 9, a, 10, c
어떻게 해야할지 감이 안잡혀 글 남깁니다. 많은 도움 부탁드립니다.
Comment 3
-
Hisory
2014.07.18 11:07
-
자리비움
2014.07.18 11:31
use tempdb
go
create table #t1 ( idx int identity not null, col1 int, col2 char(1) )
go
insert into #t1 values ( 1, 'b' )
insert into #t1 values ( 2, 'a' )
insert into #t1 values ( 3, 'a' )
insert into #t1 values ( 4, 'c' )
insert into #t1 values ( 5, 'c' )
insert into #t1 values ( 6, 'b' )
insert into #t1 values ( 7, 'b' )
insert into #t1 values ( 8, 'a' )
insert into #t1 values ( 9, 'a' )
insert into #t1 values ( 10, 'c' )
insert into #t1 values ( 11, 'a' )
insert into #t1 values ( 12, 'c' )
insert into #t1 values ( 12, 'c' )
go
declare @char1 char(1) = 'a'
declare @char2 char(1) = 'a'
declare @char3 char(1) = 'c'
select convert(varchar(20),first_row) + ',' + @char1 + ',' + convert(varchar(20),first_row + 1) + ',' + @char2 + ',' + convert(varchar(20),first_row + 2 ) + ',' + @char3
from (
select first_row = ( case when col2 = @char1 then
( case when exists ( select 1 from #t1 where idx = ( a.idx + 1 ) and col2 = @char2 ) then
( case when exists ( select 1 from #t1 where idx = ( a.idx + 2 ) and col2 = @char3 ) then idx else 0 end )
else 0 end )
else 0 end )
from #t1 as a
) as a
where a.first_row > 0
-
상어고래
2014.07.20 23:00
참고하겠습니다.
감사합니다^^
이걸 보시면 좀 감이 오시지 않을까 싶네여..
declare @Tb Table (
컬럼1 varchar(10),
컬럼2 varchar(10),
컬럼3 varchar(10)
)
Insert @Tb
Values('로우01','1','b')
,('로우02','2','a')
,('로우03','3','a')
,('로우04','4','c')
,('로우05','5','c')
,('로우06','6','b')
,('로우07','7','b')
,('로우08','8','a')
,('로우09','9','a')
,('로우10','10','c')
,('로우11','11','a')
,('로우12','12','c')
,('로우13','12','c')
Select * into TT From @Tb
/****** Object: UserDefinedFunction [dbo].[FDIV] Script Date: 07/18/2014 11:03:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create FUNCTION [dbo].[fn_t]
(@컬럼1 varchar(10))
RETURNS Varchar(10)
AS
BEGIN
Declare @Rtn Varchar(20)
Set @Rtn = ''
Select top(3) @Rtn = @Rtn + 컬럼3 + ',' From tt Where 컬럼1 >= @컬럼1
order by 컬럼1 Asc
return @Rtn
END
GO
Select * , [dbo].[fn_t](컬럼1) From tt order by 컬럼1 Asc