declare @table1 table
(
Seq int IDENTITY (1, 1) NOT NULL,
MNO int,
MNAME varchar(15),
ftype varchar(15)
)
declare @table2 table
(
MNO int,
ftype varchar(4000)
)
insert @table1 select 1, '10', '가나다'
insert @table1 select 1, '20', 'ABC'
insert @table1 select 2, '10', '123'
insert @table1 select 2, '20', '456'
insert @table1 select 2, '30', '789'
insert @table1 select 3, 'A', 'abcd'
insert @table1 select 3, 'B', 'def'
insert @table1 select 3, 'C', 'ghi'
DECLARE @Tmp_max INT
DECLARE @Tmp_cnt INT
DECLARE @Tmp_mno INT
DECLARE @Tmp_ftype varchar(15)
SET @Tmp_cnt = 1
SELECT @Tmp_max = MAX(Seq) FROM @table1
--@table2에 그룹별 문자열을 모은다.
WHILE(@Tmp_cnt <= @Tmp_max)
BEGIN
SELECT @Tmp_mno = MNO, @Tmp_ftype = ftype FROM @table1 WHERE Seq = @Tmp_cnt
IF(EXISTS(SELECT MNO FROM @table2 WHERE MNO = @Tmp_mno))
UPDATE @table2 SET ftype = ftype + '' + @Tmp_ftype WHERE MNO = @Tmp_mno
ELSE
INSERT @table2 SELECT @Tmp_mno,@Tmp_ftype
SET @Tmp_cnt = @Tmp_cnt + 1
END
--데이터 조회
SELECT a.mno,a.mname,
(
SELECT ftype FROM @table2 WHERE MNO = a.MNO
)
FROM @table1 a GROUP BY mno,mname
아래가 결과물인데 그룹별로 묶이지 않게
1/ 10/ 가나다ABC
1 /20/ 가나다ABC
2 /10/ 123456789
2 /20/123456789
2 /30/123456789
3/ A/ abcddefghi
3/ B/ abcddefghi
3 /C/ abcddefghi
아래와 같이 런식으로
1/2/3 가나다123abcd
1/2/3 가나다123ghi
1/2/3 가나다456abcd
1/2/3 가나다456def
.......................
그룹별로 묶이지 않고 중복되지 않게 나올수 있는 값이 다 나오게 연결 하려고 합니다.
답글 주시면 감사하겠습니다.