sqlunpivot示例
现在我有下表,我想取消它,所以我的列和行翻转如下-谢谢 有 想要sqlunpivot示例,sql,unpivot,Sql,Unpivot,现在我有下表,我想取消它,所以我的列和行翻转如下-谢谢 有 想要 此示例在Transact-SQL中。Oracle(11g+)语法与此类似,但还有一些选项,例如如何处理空值 -- setting up a table variable with your starting data declare @t table ( Name varchar(10) primary key ,[Age 18-24] int ,[Age 25-34]
此示例在Transact-SQL中。Oracle(11g+)语法与此类似,但还有一些选项,例如如何处理空值
-- setting up a table variable with your starting data
declare @t table
( Name varchar(10) primary key
,[Age 18-24] int
,[Age 25-34] int
,[Age 35-44] int );
insert @t (Name, [Age 18-24], [Age 25-34], [Age 35-44])
values ('John', 1, 0, 0 )
,('Maria', 0, 0, 1 )
,('June' , 0, 0, 1 )
--- the actual query starts here
select [Name], [Column], [Value]
from
( -- select relevant columns from source
select Name, [Age 18-24], [Age 25-34], [Age 35-44]
from @t
) as piv
UNPIVOT
( -- define new result columns.
---- [Value] is the detailed count/sum/whatever
---- [Column] get the old name of the column
[Value]
for [Column] in ([Age 18-24], [Age 25-34], [Age 35-44])
) as unpiv
您正在使用什么DBMS?您尝试了什么?欢迎使用堆栈溢出。您的问题没有包含足够有用的细节,我们无法帮助您。查看并了解一个项目的重要性。然后,根据需要编辑您的问题。此外,可能的副本