将多行作为一行返回

时间:2014-02-06 19:42:13

标签: sql sql-server

这是一个简单的查询,它是使用SQL Server的结果:

SELECT Notes
FROM DailyTaskHours
WHERE Notes IS NOT NULL
    AND Notes <> ''
    AND NonScrumStoryId = 2264

enter image description here

我想要返回一列的一行。我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

如果这是一个SQL Server数据库,我相信你可以用这样的方式实现PIVOT运算符:

select [1] as field1,
  [2] as field2,
  [3] as field3
from
(
  select col1, row_number() Over(order by col1) rn
  from yourtable
) src
pivot
(
  max(col1)
  for rn in ([1], [2], [3])
) piv

答案 1 :(得分:1)

我认为你需要将它们全部作为单个细胞,而不是将它们分散出去。

试试这个

With 
MyTable as
(
        Select 1 Id, 'Contacted the P6 Admin Reps to find out what to do.' Notes
Union   Select 2, 'Sent and email to request status.'
Union   Select 3, 'updated ticket and confirmed issue was resolved.'
) 
Select 
    (
        SELECT SUB.Notes + ' ' 
        FROM MyTable SUB
        FOR XML PATH('')
    ) TextValue 

以下是我得到的答案:

TextValue
---------------------------------------------------------------------------------------------------------------------------------------
Contacted the P6 Admin Reps to find out what to do. Sent and email to request status. updated ticket and confirmed issue was resolved. 

(1 row(s) affected)