本文介绍了如何将列转换为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 朋友们,我发现这个代码项目网站提供了更好的解决方案和帮助,使开发更容易,这个网站有助于与其他专业开发人员交流,以便他们可以分享他们需要它的重要知识我想说谢谢开发人员和代码项目的伟大工作。 我有一张桌子,我想将列转换为行 + - ------------ + -------------- -------------- + ------- + ------- + |(列名1)|(列名2)|(列名3)|(列名4)| + -------------- + -------------- + ------------- + - -------------- + | Data11 | Data21 | Data31 | Data41 | + -------------- + -------------- + ------------- + - -------------- + | Data12 | Data22 | Data32 | Data42 | + -------------- + -------------- + ------------- + - -------------- + | Data13 | Data23 | Data33 | Data43 | + -------------- + -------------- + ------------- + - -------------- + | Data14 | Data24 | Data34 | Data44 | + -------------- + -------------- + ------------- + - -------------- + | Data15 | Data25 | Data35 | Data45 | + -------------- + -------------- + ------------- + - -------------- + 上表列应如下表所示: + -------- + -------- + -------- + -------- + ------ - + | (Col1)| (第2栏)| (第3栏)| (Col4)| (Col5)| + -------- + -------- + -------- + -------- + -------- + |数据11 |数据12 |数据13 |数据14 |数据15 | + -------- + -------- + -------- + -------- + -------- + | Data21 |数据22 |数据23 | Data24 |数据25 | + -------- + -------- + -------- + -------- + -------- + |数据31 | Data32 | Data33 |数据34 | Data35 | + -------- + -------- + -------- + -------- + -------- + |数据41 | Data42 | Data43 | Data44 | Data45 | + -------- + -------- + -------- + -------- + -------- + 我试过下面的查询,但它只适用于单列我想要多行转换成行。 我尝试过: 声明@cols nvarchar(max)声明@query nvarchar(max) 选择@cols = stuff((选择','+ QuoteName(Row_Number()over(Order by(Select NULL)))#cols for xml path('')) ,1,1,'')选择@query ='选择*从(选择colname,RowN = Row_Number()over(按colname排序)#cols )a pivot(RowN的最大值(colname)('+ @cols +'))p' Exec sp_executesql @query 请帮助我解决问题。解决方案 我认为你想要的是一个univot而不是一个支点,下面的代码怎么样: - 创建表格和我插入前面示例中描述的值。 CREATE TABLE pvt(VendorID int ,Emp1 int ,Emp2 int , Emp3 int ,Emp4 int ,Emp5 int ); GO INSERT INTO pvt VALUES ( 1 , 4 , 3 , 5 , 4 , 4 ); INSERT INTO pvt VALUES ( 2 , 4 , 1 , 5 , 5 , 5 ); INSERT INTO pvt VALUES ( 3 , 4 , 3 , 5 , 4 , 4 ); INSERT INTO pvt VALUES ( 4 , 4 , 2 , 5 , 5 , 4 ); INSERT INTO pvt VALUES ( 5 , 5 , 1 , 5 , 5 , 5 ); GO - Unpivot桌子。 SELECT VendorID,员工,订单 FROM ( SELECT VendorID,Emp1,Emp2,Emp3,Emp4,Emp5 FROM pvt)p UNPIVOT (订单 FOR 员工 IN (Emp1,Emp2 ,Emp3,Emp4,Emp5)) AS unpvt; GO 取自此处:FROM - 使用PIVOT和UNPIVOT | Microsoft Docs [ ^ ] 这对你有用吗,如果没有,那么表的定义是什么,预期的结果,以及您正在使用的查询 - 请尝试执行上述代码段中的操作,因为如果您无法提供相关详细信息,它真的很难无法帮助... Hello friends i found that this code project sites provides better solutions and help that makes development more easy and this site helps to comunicate with other professional developers so they can share their important knowledge who requires it I want to say thanks to the developers and code project for their great work.I'm having a table in that I want to convert columns into rows+--------------+--------------+--------------+--------------+|(Column Name1)|(Column Name2)|(Column Name3)|(Column Name4)|+--------------+--------------+-------------+---------------+|Data11 |Data21 |Data31 |Data41 |+--------------+--------------+-------------+---------------+|Data12 |Data22 |Data32 |Data42 |+--------------+--------------+-------------+---------------+|Data13 |Data23 |Data33 |Data43 |+--------------+--------------+-------------+---------------+|Data14 |Data24 |Data34 |Data44 |+--------------+--------------+-------------+---------------+|Data15 |Data25 |Data35 |Data45 |+--------------+--------------+-------------+---------------+The above table column should be look like below table:+--------+--------+--------+--------+--------+| (Col1) | (Col2) | (Col3) | (Col4) | (Col5) |+--------+--------+--------+--------+--------+| Data11 | Data12 | Data13 | Data14 | Data15 |+--------+--------+--------+--------+--------+| Data21 | Data22 | Data23 | Data24 | Data25 |+--------+--------+--------+--------+--------+| Data31 | Data32 | Data33 | Data34 | Data35 |+--------+--------+--------+--------+--------+| Data41 | Data42 | Data43 | Data44 | Data45 |+--------+--------+--------+--------+--------+I tried below query but it works only for single column i want multiple columns to be converted in rows.What I have tried:Declare @cols nvarchar(max)Declare @query nvarchar(max)Select @cols = stuff((select ','+QuoteName(Row_Number() over (Order by (Select NULL))) from #cols for xml path('')),1,1,'')Select @query = ' Select * from ( Select colname, RowN = Row_Number() over (order by colname) from #cols ) a pivot (max(colname) for RowN in (' + @cols + ')) p 'Exec sp_executesql @queryPlease help me for the solution. 解决方案 I think what you want is an unpivot and not a pivot, what about the following code:-- Create the table and insert values as portrayed in the previous example.CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int);GOINSERT INTO pvt VALUES (1,4,3,5,4,4);INSERT INTO pvt VALUES (2,4,1,5,5,5);INSERT INTO pvt VALUES (3,4,3,5,4,4);INSERT INTO pvt VALUES (4,4,2,5,5,4);INSERT INTO pvt VALUES (5,5,1,5,5,5);GO-- Unpivot the table.SELECT VendorID, Employee, OrdersFROM (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5 FROM pvt) pUNPIVOT (Orders FOR Employee IN (Emp1, Emp2, Emp3, Emp4, Emp5))AS unpvt;GOtaken from here: FROM - Using PIVOT and UNPIVOT | Microsoft Docs[^]Does this work for you and if not what is the table definition, expected result, and query you are using - please try to do something like in the above code snippet because its realy difficult to impossible to help if you cannot give the relevant details... 这篇关于如何将列转换为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 15:26