zoukankan      html  css  js  c++  java
  • sql 交叉查询

    csdn  一个网友写的....

    create table #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'2005210100)
    Insert #TmpA Values('K''S'2005320200)
    Insert #TmpA Values('K''S'2005430300)
    Insert #TmpA Values('K''S'2005540400)
    Insert #TmpA Values('K''S'2005650500)
    Insert #TmpA Values('K''S'2005760600)
    Insert #TmpA Values('K''B'2005210100)
    Insert #TmpA Values('K''B'2005320200)
    Insert #TmpA Values('K''B'2005430300)
    Insert #TmpA Values('K''B'2005540400)
    Insert #TmpA Values('K''B'2005650500)
    Insert #TmpA Values('K''B'2005760600)

    //数据查询

    Declare @Sql1 Nvarchar(4000)
    Declare @Sql2 Nvarchar(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 (Select Distinct Order_month from #TmpA ) A Order By 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 (Select Distinct Order_month from #TmpA ) A Order By 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 
       
    Select DIstinct Cat2,Cat3,'Amount_A' As amount,
    (
    Select Amount_A from #TmpA Where Order_month=2 And Cat2=A.Cat2 And Cat3=A.Cat3) As '2月',
    (
    Select Amount_A from #TmpA Where Order_month=3 And Cat2=A.Cat2 And Cat3=A.Cat3) As '3月',
    (
    Select Amount_A from #TmpA Where Order_month=4 And Cat2=A.Cat2 And Cat3=A.Cat3) As '4月',
    (
    Select Amount_A from #TmpA Where Order_month=5 And Cat2=A.Cat2 And Cat3=A.Cat3) As '5月',
    (
    Select Amount_A from #TmpA Where Order_month=6 And Cat2=A.Cat2 And Cat3=A.Cat3) As '6月',
    (
    Select Amount_A from #TmpA Where Order_month=7 And Cat2=A.Cat2 And Cat3=A.Cat3) As '7月',
    (
    Select Amount_A from #TmpA Where Order_month=9 And Cat2=A.Cat2 And Cat3=A.Cat3) As '9月'
    from #TmpA A Union All 

    Select DIstinct Cat2,Cat3,'Amount_B' As amount,
    (
    Select Amount_B from #TmpA Where Order_month=2 And Cat2=A.Cat2 And Cat3=A.Cat3) As '2月',
    (
    Select Amount_B from #TmpA Where Order_month=3 And Cat2=A.Cat2 And Cat3=A.Cat3) As '3月',
    (
    Select Amount_B from #TmpA Where Order_month=4 And Cat2=A.Cat2 And Cat3=A.Cat3) As '4月',
    (
    Select Amount_B from #TmpA Where Order_month=5 And Cat2=A.Cat2 And Cat3=A.Cat3) As '5月',
    (
    Select Amount_B from #TmpA Where Order_month=6 And Cat2=A.Cat2 And Cat3=A.Cat3) As '6月',
    (
    Select Amount_B from #TmpA Where Order_month=7 And Cat2=A.Cat2 And Cat3=A.Cat3) As '7月',
    (
    Select Amount_B from #TmpA Where Order_month=9 And Cat2=A.Cat2 And Cat3=A.Cat3) As '9月'
    from #TmpA A Order By Cat2,Cat3 Desc


    我写的.

     
    create table #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 @Sql1 Nvarchar(4000)
    Declare @Sql2 Nvarchar(4000)
    Set @Sql1='Select DIstinct 学生姓名,'
    Set @Sql2='(Select 分数 from #TmpA Where 作业标题='+'''+作业标题+'''


    Select @Sql1=@Sql1+'(Select 分数 from #TmpA Where 作业标题='''+作业标题 +''' And 学生姓名=A.学生姓名 ) As '''+作业标题+''','
    from (Select Distinct 作业标题 from #TmpA ) A Order By 作业标题



    Select @Sql1=Left(@Sql1,Len(@Sql1)-1)+'from #TmpA A Order By 学生姓名 Desc'


    EXEC(@Sql1)



     
    DROP
    TABLE #TmpA
     
       Sql1 生成的sql 
    Select DIstinct 学生姓名,(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 Order By 学生姓名 Desc


    注意上边的那些引号...弄得我蛮头大的...

     
  • 相关阅读:
    what's the python之异常处理
    what's the python之面向对象(进阶)
    what's the python之面向对象
    what's the python之自定义模块和包
    Java并发编程-并发工具包(java.util.concurrent)使用指南(全)
    Java之JUC系列:外部Tools
    java CS结构软件自动升级的实现
    史上最全最强SpringMVC详细示例实战教程
    搭建最简单的SpringMVC框架(使用maven)
    小心对待query_cache_size
  • 原文地址:https://www.cnblogs.com/gwazy/p/164838.html
Copyright © 2011-2022 走看看