는 단일 셀 여기
에 값을 연결하는 Stuff()
을 사용할 수있는 것은 예입니다
create table #temp (
firstname varchar(255),
surname varchar(255),
[role] varchar(255),
[subject] varchar(255)
)
insert into #temp
values ('Jane', 'Smith', 'Maths Teacher', 'Math'),
('Jane', 'Smith', 'Maths Teacher', 'Physics'),
('Jane', 'Smith', 'Maths Teacher', 'Tutorial'),
('Jane', 'Smith', 'Physics Teacher', 'Math'),
('Jane', 'Smith', 'Physics Teacher', 'Physics'),
('Jane', 'Smith', 'Physics Teacher', 'Tutorial'),
('Kate', 'Smith', 'Maths Teacher', 'Math1'),
('Kate', 'Smith', 'Maths Teacher', 'Physics'),
('Kate', 'Smith', 'Maths Teacher', 'Tutoria'),
('Kate', 'Smith', 'Physics Teacher', 'Math'),
('Kate', 'Smith', 'Physics Teacher', 'Physics'),
('Kate', 'Smith', 'Physics Teacher', 'Tutorial')
select * from #temp
select distinct firstname,surname, STUFF((SELECT distinct ',' + [role]
FROM [#temp] t1
where t1.firstname = t2.firstname and t1.surname = t2.surname
FOR XML PATH('')),
1, 1, ''),
STUFF((SELECT distinct ',' + [subject]
FROM #temp t1
where t1.firstname = t2.firstname and t1.surname = t2.surname
FOR XML PATH('')),
1, 1, '') as [subject]
from #temp t2
drop table #temp
이가하는 질문을 수정하시기 바랍니다 Cross APPLY
create table #temp (
firstname varchar(255),
surname varchar(255),
[role] varchar(255),
[subject] varchar(255)
)
insert into #temp
values ('Jane', 'Smith', 'Maths Teacher', 'Math'),
('Jane', 'Smith', 'Maths Teacher', 'Physics'),
('Jane', 'Smith', 'Maths Teacher', 'Tutorial'),
('Jane', 'Smith', 'Physics Teacher', 'Math'),
('Jane', 'Smith', 'Physics Teacher', 'Physics'),
('Jane', 'Smith', 'Physics Teacher', 'Tutorial'),
('Kate', 'Smith', 'Maths Teacher', 'Math1'),
('Kate', 'Smith', 'Maths Teacher', 'Physics'),
('Kate', 'Smith', 'Maths Teacher', 'Tutoria'),
('Kate', 'Smith', 'Physics Teacher', 'Math'),
('Kate', 'Smith', 'Physics Teacher', 'Physics'),
('Kate', 'Smith', 'Physics Teacher', 'Tutorial')
select distinct firstname,surname,rol.[role], sub.subject
from #temp t2
CROSS APPLY (SELECT convert(varchar(20), [role]) + ','
FROM #temp t1
where t1.firstname = t2.firstname and t1.surname = t2.surname
GROUP BY firstname,surname,[role]
FOR XML PATH('')) rol([role])
CROSS APPLY (SELECT convert(varchar(20), [subject]) +','
FROM #temp t1
where t1.firstname = t2.firstname and t1.surname = t2.surname
GROUP BY firstname,surname,[subject]
FOR XML PATH('')) sub([subject])
drop table #temp
를 사용하여 achive 수있는 또 다른 방법 [** sample data **] (http://plaintexttools.github.io/plain-text-table/) 및 해당 데이터를 기반으로 예상되는 출력을 추가하십시오. 그것들을 [** 포맷 된 텍스트 **] (http://stackoverflow.com/help/formatting) 및 엄격하게 [** 스크린 샷 없음 **] (http://meta.stackoverflow.com/questions/285551/)으로 제공하십시오. 왜 코드를 업로드하지 않을 수 있습니까? - 질문을 할 때 이미지 이미지/285557 # 285557). ** 코멘트에 코드 또는 추가 정보를 게시하지 마십시오. [** 최소, 완전하고 검증 가능한 예제 **] (https://stackoverflow.com/help/mcve)를 가지고 있는지 확인하십시오. – SriniV
사용중인 데이터베이스에 질문에 태그를 답니다. –
그런 가치를 저장하는 것은 나쁜 생각 일뿐입니다. 또한 사용중인 데이터베이스를 설명하는 태그를 추가하십시오. – PacoDePaco