本文介绍了如何使用unpivot将Colums转换为sql server中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 表格就像这样... 创建 table DailyIncome(VendorId nvarchar ( 10 ), IncomeDay nvarchar ( 10 ),IncomeAmount int ) 插入 进入 DailyIncome 值(SPIKE,FRI, 100 ) insert into DailyIncome values (SPIKE,MON, 300 ) 插入 进入 DailyIncome 值(FREDS, SUN, 400 ) insert 进入 DailyIncome 值(SPIKE,WED, 500 ) 插入 进入 DailyIncome 值(SPIKE,TUE, 200 ) insert into DailyIncome 值(JOHNS,WED, 900 ) 插入 进入 DailyIncome 值(SPIKE,FRI , 100 ) insert into DailyIncome 值(JOHNS,MON, 300 ) 插入 进入 DailyIncome values (SPIKE,SUN, 400 ) insert 进入 DailyIncome 值(JOHNS,FRI, 300 ) 插入 进入 DailyIncome 值(FREDS,TUE, 500 ) insert into DailyIncome values (FREDS,TUE, 200 ) 插入 进入 DailyIncome 值(SPIKE, MON, 900 ) 插入 进入 DailyIncome 值(FREDS,FRI, 900 ) 插入 进入 DailyIncome values (FREDS,MON, 500 ) insert into DailyIncome 值(JOHNS,SUN, 600 ) 插入 进入 DailyIncome 值(SPIKE,FRI, 300 ) insert into DailyIncome values (SPIKE,WED, 500 ) 插入 进入 DailyIncome 值(SPIKE, FRI, 300 ) then我希望列到行和列到列... 解决方案 SELECT VendorId,SUN,MON,TUE,WED,THU,FRI,SAT FROM ( SELECT VendorId,IncomeDay,IncomeAmount FROM DailyIncome)up pivot(AVG(IncomeAmount) FOR IncomeDay in (SUN,MON,TUE,WED,THU,FRI,SAT)) AS pvt Table iS like this...create table DailyIncome(VendorId nvarchar(10), IncomeDay nvarchar(10), IncomeAmount int)insert into DailyIncome values (SPIKE, FRI, 100)insert into DailyIncome values (SPIKE, MON, 300)insert into DailyIncome values (FREDS, SUN, 400)insert into DailyIncome values (SPIKE, WED, 500)insert into DailyIncome values (SPIKE, TUE, 200)insert into DailyIncome values (JOHNS, WED, 900)insert into DailyIncome values (SPIKE, FRI, 100)insert into DailyIncome values (JOHNS, MON, 300)insert into DailyIncome values (SPIKE, SUN, 400)insert into DailyIncome values (JOHNS, FRI, 300)insert into DailyIncome values (FREDS, TUE, 500)insert into DailyIncome values (FREDS, TUE, 200)insert into DailyIncome values (SPIKE, MON, 900)insert into DailyIncome values (FREDS, FRI, 900)insert into DailyIncome values (FREDS, MON, 500)insert into DailyIncome values (JOHNS, SUN, 600)insert into DailyIncome values (SPIKE, FRI, 300)insert into DailyIncome values (SPIKE, WED, 500)insert into DailyIncome values (SPIKE, FRI, 300)then i want columns to rows and rows to colums... 解决方案 SELECT VendorId,SUN,MON,TUE,WED,THU,FRI,SAT FROM(SELECT VendorId,IncomeDay,IncomeAmount FROM DailyIncome) uppivot(AVG(IncomeAmount) FOR IncomeDay in(SUN,MON,TUE,WED,THU,FRI,SAT))AS pvt 这篇关于如何使用unpivot将Colums转换为sql server中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 08:52