2017-03-02 1 views
0

나는 적은 행이 두 번째 조각으로,이 쿼리의 출력을 변경하려면 :SQL Server에서 쿼리를 연결하고 가로 채는 방법?

id row_id element_id container_id text    answer 
-------------------------------------------------------------------- 
1  1  10   100   Question1:   Yes 
1  1  10   100   Did you check... Yes 
1  2  10   100   Question2:   Yes 
1  2  10   100   Did you verify... Yes 

원하는 출력 :

id row_id element_id container_id Question       answer 
---------------------------------------------------------------------------- 
1 1  10   100   Question1: Did you check..  Yes 
1 2  10   100   Question2: Did you verify...  Yes 

따라서 테이블이 짧아. 이 기능은 MySQL에서 group_concat 기능으로 수행 할 수 있지만 필자는 필자의 요구에 정확하게 적용하지 못했습니다.

도움이 될 것입니다. (? 중복)

여기에 질문이

+1

을 (대답없는 질문에 대한 테스트, 왼쪽 가입해야 할 수도 있습니다) ... "질문 1로 이동하지 않습니까? 질문 2를 자연 순서대로 살펴 보았지만 두 번째 대답 "확인 했습니까 ..."가 질문 2로 이동한다는 것을 쿼리에 어떻게 알릴 수 있습니까? 질문 1이 아닙니다. 이 문제는 응답 열까지 확장됩니다. 해결 방법 - "Question1 :"및 "Check you ..."라는 텍스트 값이 다른 자식 테이블 또는 조회 테이블에 있으면 원하는 것을 얻을 수 있습니다. –

+0

안녕하세요 @SqlSurfer, 지적 해 주셔서 고맙습니다. 나는 구별되는 란을 포함하는 것을 잊었다. 나는 내 사건이 무엇인지 보여주는 코드를 업데이트했다. – rafacardosoc

+1

http://stackoverflow.com/questions/10553198/merge-multiple-rows-into-a-single-row/10553404#10553404 – SQLMason

답변

1

이 그대로 질문에 대한 답 - 나는 데이터베이스 테이블 구조에서 더 공식적인 수정 프로그램을 추천 할 것입니다 또는 데이터를 채우는 삽입 및 갱신 명령.의 수정. 당신은 "당신이 확인했다는 것을 어떻게 알 수 있습니까 : 문제를 선물로 여기"붙어 "요점은,

SELECT q.id 
     , q.row_id 
     , q.container_id 
     , q.Text 
     , q.answer 
     , a.answer 
     , a.Text 
     , q.Text + ' ' + a.Text as SqlFor300Alex 
    FROM tblQandA q 
    INNER JOIN tblQandA a on q.id = a.id 
         and q.row_id = a.row_id 
         and q.element_id = a.element_id 
         and q.container_id = a.container_id 
         and a.Text NOT Like('Question%') 
    WHERE q.Text Like('Question%') 
+0

본인의 질문이 텍스트보다 확실하기 때문에 나는 당신을 더 좋아합니다. IDK는 항상 2 개가 있고 만약 있다면 그가 두 개의 기록을 원하겠습니까? 나는 아마 " – SQLMason

1

을 매우 비슷한에 내 대답을 참조하십시오 감사합니다 Merge multiple rows into a single row

SELECT [id], 
     [row_id], 
     [element_id], 
     [container_id], 
     (
      SELECT STUFF((
          SELECT [text] 
          FROM @table 
          WHERE [ID] = tbl.[ID] 
            AND [row_ID] = tbl.[row_ID] 
            AND [element_id] = tbl.[element_id] 
            AND [container_id] = tbl.[container_id] 
            and [answer] = tbl.[answer] 
          GROUP BY [text] 
          FOR 
          XML PATH('') 
         ), 0, 0, '') -- you may have to change to 1,0 or 1,1 
     ) Question, 
     [answer] 
FROM @table tbl 
GROUP BY [id], 
     [row_id], 
     [element_id], 
     [container_id], 
     [answer] 
0

이 시도

declare @t table(id int,row_id int,element_id int,container_id int, texts varchar(500) 
, answer varchar(500),rowtype int) 
insert into @t VALUES 
(1,1,10,100,'Question1:','Yes',1) 
,(1,1,10,100,'Did you check...','Yes',2) 
,(1,2,10,100,'Question2: ','Yes',1) 
,(1,2,10,100,'Did you verify..','Yes',2) 

select id,row_id,element_id,container_id,texts 
+' '+(select top 1 texts from @t b 
where b.row_id=a.row_id and b.rowtype=2) 
,answer 
from @t a 
where rowtype=1 

;With CTE as 
(
select id,row_id,element_id,container_id,texts,answer 
,case when CHARINDEX('Question',texts)>0 
then 1 else 2 end Rowtype from @t 
) 
select id,row_id,element_id,container_id,texts 
+' '+(select top 1 texts from cte b 
where b.row_id=a.row_id and b.rowtype=2) 
,answer 
from cte a 
where rowtype=1 
관련 문제