有没有办法获取这些数据,旋转它然后将它组合成一个字符串..?
Is there a way to take this data, pivot it and then combine it into a single string..?
我有一个 table,其中包含如下数据:
+=========+============+=========+==============+
| num | module-id | page-no | txt |
+=========+============+=========+==============+
| 3177564 | 1 | 1 |text string 1 |
+---------+------------+---------+--------------+
| 3177564 | 1 | 2 |text string 2 |
+---------+------------+---------+--------------+
| 3177564 | 1 | 3 |text string 3 |
+---------+------------+---------+--------------+
| 3177564 | 2 | 1 |text string 1 |
+---------+------------+---------+--------------+
| 3177564 | 2 | 2 |text string 2 |
+---------+------------+---------+--------------+
| 3177564 | 3 | 1 |text string 1 |
+---------+------------+---------+--------------+
| 3177564 | 3 | 2 |text string 2 |
+---------+------------+---------+--------------+
| 3177564 | 3 | 3 |text string 3 |
+---------+------------+---------+--------------+
然后我可以使用 PIVOT 函数生成以下内容:
+=========+============+==============+==============+==============+
| num | module-id | page-no-1 | page-no-2 | page-no-3 |
+=========+============+==============+==============+==============+
| 3177564 | 1 |text string 1 |text string 2 |text string 3 |
+---------+------------+--------------+--------------+--------------+
| 3177564 | 2 |text string 1 |text string 2 |NULL |
+---------+------------+--------------+--------------+--------------+
| 3177564 | 3 |text string 1 |text string2 |text string 3 |
+---------+------------+--------------+--------------+--------------+
用于生成枢轴的代码是:
SELECT *
FROM
(
SELECT
j.num
,jpad.[module-id]
,jpad.[page-no]
,jpad.txt
FROM
job j
LEFT OUTER JOIN jobpad jpad ON jpad.num = j.num
WHERE
(j.num IN
('3177564'))
AND (jpad.[page-no] IN (1,2,3,4,5,6,7,8,9,10)) a
PIVOT
(
MAX(a.txt) FOR a.[page-no] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])
) p
ORDER BY num
这很好,但是我想做的是尽可能按照以下示例连接数据:
请注意 p-1、p-2 等指的是页码。
+=========+=========================================================+=============+=============+
| num | module-id-1 | module-id-2 | module-id-3 |
+=========+=========================================================+=============+=============+
| 3177564 |p-1 text string 1 & p-2 text string 2 & p-3 text string 3|per module 1 |per module 1 |
+---------+---------------------------------------------------------+-------------+-------------+
我已经考虑了一段时间了,但脑子里想不出来。
我目前使用 SQL Server 2014。
要获取您要查找的第一列:
; -- see sqlblog.org/cte
WITH src AS
(
SELECT num, [module-id], txt = STUFF(
(SELECT CONCAT(' & ', 'p-', [page-no], ' ', txt)
FROM dbo.StuffAndThings WHERE num = s.num
AND [module-id] = s.[module-id]
FOR XML PATH(''),
TYPE).value(N'./text()[1]', N'varchar(max)'), 1, 3, '')
FROM dbo.StuffAndThings AS s
WHERE [page-no] = 1
)
SELECT num,
[module-id-1] = [1],
[module-id-2] = [2],
[module-id-3] = [3]
FROM src PIVOT
(MAX(txt) FOR [module-id] IN ([1],[2],[3])) AS p;
我有一个 table,其中包含如下数据:
+=========+============+=========+==============+
| num | module-id | page-no | txt |
+=========+============+=========+==============+
| 3177564 | 1 | 1 |text string 1 |
+---------+------------+---------+--------------+
| 3177564 | 1 | 2 |text string 2 |
+---------+------------+---------+--------------+
| 3177564 | 1 | 3 |text string 3 |
+---------+------------+---------+--------------+
| 3177564 | 2 | 1 |text string 1 |
+---------+------------+---------+--------------+
| 3177564 | 2 | 2 |text string 2 |
+---------+------------+---------+--------------+
| 3177564 | 3 | 1 |text string 1 |
+---------+------------+---------+--------------+
| 3177564 | 3 | 2 |text string 2 |
+---------+------------+---------+--------------+
| 3177564 | 3 | 3 |text string 3 |
+---------+------------+---------+--------------+
然后我可以使用 PIVOT 函数生成以下内容:
+=========+============+==============+==============+==============+
| num | module-id | page-no-1 | page-no-2 | page-no-3 |
+=========+============+==============+==============+==============+
| 3177564 | 1 |text string 1 |text string 2 |text string 3 |
+---------+------------+--------------+--------------+--------------+
| 3177564 | 2 |text string 1 |text string 2 |NULL |
+---------+------------+--------------+--------------+--------------+
| 3177564 | 3 |text string 1 |text string2 |text string 3 |
+---------+------------+--------------+--------------+--------------+
用于生成枢轴的代码是:
SELECT *
FROM
(
SELECT
j.num
,jpad.[module-id]
,jpad.[page-no]
,jpad.txt
FROM
job j
LEFT OUTER JOIN jobpad jpad ON jpad.num = j.num
WHERE
(j.num IN
('3177564'))
AND (jpad.[page-no] IN (1,2,3,4,5,6,7,8,9,10)) a
PIVOT
(
MAX(a.txt) FOR a.[page-no] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])
) p
ORDER BY num
这很好,但是我想做的是尽可能按照以下示例连接数据:
请注意 p-1、p-2 等指的是页码。
+=========+=========================================================+=============+=============+
| num | module-id-1 | module-id-2 | module-id-3 |
+=========+=========================================================+=============+=============+
| 3177564 |p-1 text string 1 & p-2 text string 2 & p-3 text string 3|per module 1 |per module 1 |
+---------+---------------------------------------------------------+-------------+-------------+
我已经考虑了一段时间了,但脑子里想不出来。
我目前使用 SQL Server 2014。
要获取您要查找的第一列:
; -- see sqlblog.org/cte
WITH src AS
(
SELECT num, [module-id], txt = STUFF(
(SELECT CONCAT(' & ', 'p-', [page-no], ' ', txt)
FROM dbo.StuffAndThings WHERE num = s.num
AND [module-id] = s.[module-id]
FOR XML PATH(''),
TYPE).value(N'./text()[1]', N'varchar(max)'), 1, 3, '')
FROM dbo.StuffAndThings AS s
WHERE [page-no] = 1
)
SELECT num,
[module-id-1] = [1],
[module-id-2] = [2],
[module-id-3] = [3]
FROM src PIVOT
(MAX(txt) FOR [module-id] IN ([1],[2],[3])) AS p;