2

我有一个查询,它返回一些看起来像这样的数据:

Description
----------
Thing1
Thing2
Thing3
Thing4
Thing5
Thing6
Thing7

我想让数据看起来像这样:

Desc1    Desc2  Desc3
-----    -----  -----
Thing1   Thing2 Thing3
Thing4   Thing5 Thing6
Thing7

有人可以提供一个如何做到这一点的例子吗?

谢谢!

4

3 回答 3

7

您说Desc列中数据的顺序无关紧要。

如果是这种情况,那么您可以使用以下 which 实现NTILEand row_number()

;with cte as
(
  select description,
    'desc'+cast(ntile(3) over(order by Description) as varchar(10)) col
  from yt
)
select desc1, desc2, desc3
from
(
  select description, col,
    row_number() over(partition by col order by col) rn
  from cte
) d
pivot
(
  max(description)
  for col in (desc1, desc2, desc3)
) piv;

请参阅SQL Fiddle with Demo

NTILE函数将行分配到单独的组中。完成后,row_number()在分组时应用 a 为每一行提供一个唯一编号。

这给出了一个结果:

|  DESC1 |  DESC2 |  DESC3 |
----------------------------
| Thing1 | Thing4 | Thing6 |
| Thing2 | Thing5 | Thing7 |
| Thing3 | (null) | (null) |
于 2013-04-08T20:23:01.090 回答
3

这样做的另一种方法,如果顺序确实重要,并跳过 PIVOT 功能:

WITH CTE AS 
(
    SELECT 
        (ROW_NUMBER() OVER (ORDER BY [Description])+2) /3 AS RowID,
        (ROW_NUMBER() OVER (ORDER BY [Description])+2) % 3 +1 AS ColID,  
        [Description] 
    FROM Table1
)
SELECT 
     MIN(CASE WHEN ColID = 1 THEN [Description] END) DESC1
    ,MIN(CASE WHEN ColID = 2 THEN [Description] END) DESC2
    ,MIN(CASE WHEN ColID = 3 THEN [Description] END) DESC3
FROM CTE
GROUP BY RowID

SQLFiddle 演示

于 2013-04-08T20:33:54.270 回答
1

我使用来自@NenadZivkovic 的 sqlfiddle 的基本数据来提供不同的解决方案

http://www.sqlfiddle.com/#!6/b676a/15

WITH Numbered AS 
(
  SELECT 
    ROW_NUMBER() OVER (ORDER BY Description) as rn,
    (ROW_NUMBER() OVER (ORDER BY Description) - 1) % 3 as colid,
    (ROW_NUMBER() OVER (ORDER BY Description) - 1) / 3 as line,
    Description 
  FROM Table1
)
SELECT desc1.description as DESC1, desc2.description as DESC2, desc3.description as DESC3
FROM   (SELECT line, description FROM Numbered WHERE colid=0) as desc1
LEFT JOIN 
       (SELECT line, description FROM Numbered WHERE colid=1) as desc2
ON (desc1.line = desc2.line)
LEFT JOIN 
       (SELECT line, description FROM Numbered WHERE colid=2) as desc3
ON (desc2.line = desc3.line)
于 2013-04-08T21:17:06.693 回答