问题描述
表转换前的样式产品ID号CPU内存显示卡1471.8nullnull150nullnullGF4TAI82000163双核1.9512MGF3TAI4000怎么转成下面这样的表。产品ID号147150163CPU1.8null双核1.9内存nullnull512M显示卡nullGF4TAI82000GF3TAI4000代码怎么写。急急急
解决方案
解决方案二:
行列都是不固定的,动态的
解决方案三:
动态的关注
解决方案四:
我也想学
解决方案五:
/*普通行列转换(2007-11-18于海南三亚)假设有张学生成绩表(tb)如下:NameSubjectResult张三语文 74张三数学 83张三物理 93李四语文 74李四数学 84李四物理 94*/-------------------------------------------------------------------------/*想变成姓名语文数学物理-------------------------------------------李四748494张三748393*/createtabletb(Namevarchar(10),Subjectvarchar(10),Resultint)insertintotb(Name,Subject,Result)values('张三','语文',74)insertintotb(Name,Subject,Result)values('张三','数学',83)insertintotb(Name,Subject,Result)values('张三','物理',93)insertintotb(Name,Subject,Result)values('李四','语文',74)insertintotb(Name,Subject,Result)values('李四','数学',84)insertintotb(Name,Subject,Result)values('李四','物理',94)go--静态SQL,指subject只有语文、数学、物理这三门课程。selectname姓名,max(casesubjectwhen'语文'thenresultelse0end)语文,max(casesubjectwhen'数学'thenresultelse0end)数学,max(casesubjectwhen'物理'thenresultelse0end)物理fromtbgroupbyname/*姓名语文数学物理-------------------------------------------李四748494张三748393*/--动态SQL,指subject不止语文、数学、物理这三门课程。declare@sqlvarchar(8000)set@sql='selectNameas'+'姓名'select@sql=@sql+',max(caseSubjectwhen'''+Subject+'''thenResultelse0end)['+Subject+']'from(selectdistinctSubjectfromtb)asaset@sql=@sql+'fromtbgroupbyname'exec(@sql)/*姓名数学物理语文-------------------------------------------李四849474张三839374*/-------------------------------------------------------------------/*加个平均分,总分姓名语文数学物理平均分总分--------------------------------------------------------------------------李四74849484.00252张三74839383.33250*/--静态SQL,指subject只有语文、数学、物理这三门课程。selectname姓名,max(casesubjectwhen'语文'thenresultelse0end)语文,max(casesubjectwhen'数学'thenresultelse0end)数学,max(casesubjectwhen'物理'thenresultelse0end)物理,cast(avg(result*1.0)asdecimal(18,2))平均分,sum(result)总分fromtbgroupbyname/*姓名语文数学物理平均分总分--------------------------------------------------------------------------李四74849484.00252张三74839383.33250*/--动态SQL,指subject不止语文、数学、物理这三门课程。declare@sql1varchar(8000)set@sql1='selectNameas'+'姓名'select@sql1=@sql1+',max(caseSubjectwhen'''+Subject+'''thenResultelse0end)['+Subject+']'from(selectdistinctSubjectfromtb)asaset@sql1=@sql1+',cast(avg(result*1.0)asdecimal(18,2))平均分,sum(result)总分fromtbgroupbyname'exec(@sql1)/*姓名数学物理语文平均分总分--------------------------------------------------------------------------李四84947484.00252张三83937483.33250*/droptabletb------------------------------------------------------------------------------------------------------------------/*如果上述两表互相换一下:即姓名语文数学物理张三74 83 93李四74 84 94想变成NameSubjectResult----------------------------李四语文74李四数学84李四物理94张三语文74张三数学83张三物理93*/createtabletb1(姓名varchar(10),语文int,数学int,物理int)insertintotb1(姓名,语文,数学,物理)values('张三',74,83,93)insertintotb1(姓名,语文,数学,物理)values('李四',74,84,94)select*from(select姓名asName,Subject='语文',Result=语文fromtb1unionallselect姓名asName,Subject='数学',Result=数学fromtb1unionallselect姓名asName,Subject='物理',Result=物理fromtb1)torderbyname,caseSubjectwhen'语文'then1when'数学'then2when'物理'then3when'总分'then4end--------------------------------------------------------------------/*加个平均分,总分NameSubjectResult-------------------------------------李四语文74.00李四数学84.00李四物理94.00李四平均分84.00李四总分252.00张三语文74.00张三数学83.00张三物理93.00张三平均分83.33张三总分250.00*/select*from(select姓名asName,Subject='语文',Result=语文fromtb1unionallselect姓名asName,Subject='数学',Result=数学fromtb1unionallselect姓名asName,Subject='物理',Result=物理fromtb1unionallselect姓名asName,Subject='平均分',Result=cast((语文+数学+物理)*1.0/3asdecimal(18,2))fromtb1unionallselect姓名asName,Subject='总分',Result=语文+数学+物理fromtb1)torderbyname,caseSubjectwhen'语文'then1when'数学'then2when'物理'then3when'平均分'then4when'总分'then5enddroptabletb1