[英]Transposing Rows into Multiple columns SQL Server
I have a table with multiple rows that needs transposed to one row with multiple columns.我有一个多行的表格,需要转置为多列的一行。 The order in which the items are listed on my current table matters for ranking purposes, so I need them to stay in order when they are transposed into columns
项目在当前表中列出的顺序对排名很重要,因此我需要它们在转换为列时保持顺序
Thanks in advance提前致谢
You can use row_number()
& do conditional aggregation :您可以使用
row_number()
并进行条件聚合:
SELECT individuaid,
MAX(CASE WHEN seq = 1 THEN item_no END) item_no_1,
MAX(CASE WHEN seq = 1 THEN HL2_CODE END) HL_2_1,
. . . .
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY individuaid ORDER BY item_no) AS seq
FROM table t
) T
GROUP BY individuaid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.