如何在SQL Server中连接和横向查询?

时间:2017-03-02 00:32:09

标签: 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函数完成,但我无法正确应用它。

任何帮助都会很棒。

谢谢

3 个答案:

答案 0 :(得分: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]

答案 1 :(得分:1)

这确实回答了这个问题 - 我建议在数据库表结构中更正式的修复或修改填充数据的insert和update语句。 (针对未回答的问题进行测试,可能需要左连接)

 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%') 

答案 2 :(得分: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