createtable #TmpA ( Cat2 varchar(32), Cat3 varchar(32), Order_year int, Order_month int, Amount_A decimal(18,2), Amount_B decimal(18,2) ) --插入数据 Insert #TmpA Values('K', 'S', 2005, 2, 10, 100) Insert #TmpA Values('K', 'S', 2005, 3, 20, 200) Insert #TmpA Values('K', 'S', 2005, 4, 30, 300) Insert #TmpA Values('K', 'S', 2005, 5, 40, 400) Insert #TmpA Values('K', 'S', 2005, 6, 50, 500) Insert #TmpA Values('K', 'S', 2005, 7, 60, 600) Insert #TmpA Values('K', 'B', 2005, 2, 10, 100) Insert #TmpA Values('K', 'B', 2005, 3, 20, 200) Insert #TmpA Values('K', 'B', 2005, 4, 30, 300) Insert #TmpA Values('K', 'B', 2005, 5, 40, 400) Insert #TmpA Values('K', 'B', 2005, 6, 50, 500) Insert #TmpA Values('K', 'B', 2005, 7, 60, 600) //数据查询 Declare@Sql1Nvarchar(4000) Declare@Sql2Nvarchar(4000) Set@Sql1='Select DIstinct Cat2,Cat3,''Amount_A'' As amount,' Set@Sql2='Select DIstinct Cat2,Cat3,''Amount_B'' As amount,' Select@Sql1=@Sql1+'(Select Amount_A from #TmpA Where Order_month='+Rtrim(Order_month)+' And Cat2=A.Cat2 And Cat3=A.Cat3) As '''+Rtrim(Order_month)+N'月'',' from (SelectDistinct Order_month from #TmpA ) A OrderBy Order_month Select@Sql2=@Sql2+'(Select Amount_B from #TmpA Where Order_month='+Rtrim(Order_month)+' And Cat2=A.Cat2 And Cat3=A.Cat3) As '''+Rtrim(Order_month)+N'月'',' from (SelectDistinct Order_month from #TmpA ) A OrderBy Order_month Select@Sql1=Left(@Sql1,Len(@Sql1)-1)+'from #TmpA A Union All '+Left(@Sql2,Len(@Sql2)-1)+'from #TmpA A Order By Cat2,Cat3 Desc' EXEC(@Sql1)
Sql1 生成的sql
SelectDIstinct Cat2,Cat3,'Amount_A'As amount, (Select Amount_A from #TmpA Where Order_month=2And Cat2=A.Cat2 And Cat3=A.Cat3) As'2月', (Select Amount_A from #TmpA Where Order_month=3And Cat2=A.Cat2 And Cat3=A.Cat3) As'3月', (Select Amount_A from #TmpA Where Order_month=4And Cat2=A.Cat2 And Cat3=A.Cat3) As'4月', (Select Amount_A from #TmpA Where Order_month=5And Cat2=A.Cat2 And Cat3=A.Cat3) As'5月', (Select Amount_A from #TmpA Where Order_month=6And Cat2=A.Cat2 And Cat3=A.Cat3) As'6月', (Select Amount_A from #TmpA Where Order_month=7And Cat2=A.Cat2 And Cat3=A.Cat3) As'7月', (Select Amount_A from #TmpA Where Order_month=9And Cat2=A.Cat2 And Cat3=A.Cat3) As'9月' from #TmpA A UnionAll SelectDIstinct Cat2,Cat3,'Amount_B'As amount, (Select Amount_B from #TmpA Where Order_month=2And Cat2=A.Cat2 And Cat3=A.Cat3) As'2月', (Select Amount_B from #TmpA Where Order_month=3And Cat2=A.Cat2 And Cat3=A.Cat3) As'3月', (Select Amount_B from #TmpA Where Order_month=4And Cat2=A.Cat2 And Cat3=A.Cat3) As'4月', (Select Amount_B from #TmpA Where Order_month=5And Cat2=A.Cat2 And Cat3=A.Cat3) As'5月', (Select Amount_B from #TmpA Where Order_month=6And Cat2=A.Cat2 And Cat3=A.Cat3) As'6月', (Select Amount_B from #TmpA Where Order_month=7And Cat2=A.Cat2 And Cat3=A.Cat3) As'7月', (Select Amount_B from #TmpA Where Order_month=9And Cat2=A.Cat2 And Cat3=A.Cat3) As'9月' from #TmpA A OrderBy Cat2,Cat3 Desc
我写的.
createtable #TmpA ( 学生姓名 varchar(32), 作业标题 VARCHAR(30), 分数 decimal(18,2), ) --插入数据 Insert #TmpA Values( 'S', '作业1', 10) Insert #TmpA Values( 'S', '作业2', 20) Insert #TmpA Values( 'S', '作业3', 30) Insert #TmpA Values( 'S', '作业4', 40) Insert #TmpA Values( 'B', '作业1', 10) Insert #TmpA Values( 'B', '作业2', 20) Insert #TmpA Values( 'B', '作业3', 30) Insert #TmpA Values( 'B', '作业4', 40) Insert #TmpA Values( 'B', '作业6', 40) --数据查询 Declare@Sql1Nvarchar(4000) Declare@Sql2Nvarchar(4000) Set@Sql1='Select DIstinct 学生姓名,' Set@Sql2='(Select 分数 from #TmpA Where 作业标题='+'''+作业标题+''' Select@Sql1=@Sql1+'(Select 分数 from #TmpA Where 作业标题='''+作业标题 +''' And 学生姓名=A.学生姓名 ) As '''+作业标题+''',' from (SelectDistinct 作业标题 from #TmpA ) A OrderBy 作业标题 Select@Sql1=Left(@Sql1,Len(@Sql1)-1)+'from #TmpA A Order By 学生姓名 Desc' EXEC(@Sql1) DROP TABLE #TmpA
Sql1 生成的sql
SelectDIstinct 学生姓名,(Select 分数 from #TmpA Where 作业标题='作业1'And 学生姓名=A.学生姓名 ) As'作业1',(Select 分数 from #TmpA Where 作业标题='作业2'And 学生姓名=A.学生姓名 ) As'作业2',(Select 分数 from #TmpA Where 作业标题='作业3'And 学生姓名=A.学生姓名 ) As'作业3',(Select 分数 from #TmpA Where 作业标题='作业4'And 学生姓名=A.学生姓名 ) As'作业4',(Select 分数 from #TmpA Where 作业标题='作业6'And 学生姓名=A.学生姓名 ) As'作业6'from #TmpA A OrderBy 学生姓名 Desc