不好意思,补充一个cx也可以在多个ljdh中出现

解决方案 »

  1.   


    declare @s1 varchar(8000),@s2 varchar(8000)
    select @s1='',@s2=''
    select @s1=@s1+',['+cx+']=sum(case cx when '''+cx+''' then s1 else '''' end)'
    from 表一
    group by cx
    select @s2=@s2+',['+bm+']=max(case cx when '''+bm+''' then s1 else '''' end)'
    from 表二
    group by bm
    exec('select ljdh'+@s1+@s2+' from(
    select ljdh,cx,sl=cast(sl as varchar) from 表一
    union all
    select ljdh,bm,bm from 表二
    )a ')
      

  2.   

    很是谢谢你啦,不好意思,我实在看不懂最后的的执行语句,
    最主要的是,运行的时候提示出错:
    服务器: 消息 105,级别 15,状态 1,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]字符串 'G7 from(
    select lbjdh,cx dtde=cast(dtde as varchar) from bomggwlqd
    union all
    select lbjdh,bm,bm from bomgymx
    )a ' 之前有未闭合的引号。
    服务器: 消息 170,级别 15,状态 1,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: 'G7 from(
    select lbjdh,cx dtde=cast(dtde as varchar) from bomggwlqd
    union all
    select lbjdh,bm,bm from bomgymx
    )a ' 附近有语法错误。这是什么意思呀?
      

  3.   

    服务器: 消息 105,级别 15,状态 1,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]字符串 'G7 from(
    select lbjdh,cx dtde=cast(dtde as varchar) from bomggwlqd
    union all
    select lbjdh,bm,bm from bomgymx
    )a ' 之前有未闭合的引号。
    检查一下你的SQL语句里面是不是有字符类型的字段的引号少了
      

  4.   

    拜托,我还是没有搞明白耶,
    zjcxc(邹建)的回复中:
    select @s2=@s2+',['+bm+']=max(case cx when '''+bm+''' then s1 else '''' end)'
    from 表二
    group by bm这是什么意思呀?表二中怎么有了cx 和sl 这是怎么回事呀?是我太笨?
    拜托zjcxc(邹建),我知道你很厉害,你是不是没有看清楚我的要求呀?
    再帮帮忙吧。