表一
    ID  Name  Sex    N1   N2   N3   N4  N5  N6 .... N50
    1   张三  男     
    2   李四  女
    3   王五  男表二
    ID  TZ   TZName  TZValue
    1   N1   班级    一班
    2   N2   年龄    18
    3   N3   地址    中国问题---
      我要的结果是
    ID  Name  Sex    班级   年龄   地址  
    1   张三  男     一班   18     中国
    2   李四  女     一班   18     中国
    3   王五  男     一班   18     中国
得到这样的表?注释:表二是可以插入数据的 如 N4 喜好 篮球  结果也要增加‘喜好’一列   

解决方案 »

  1.   

    楼主去搜一下交叉表方面的资料
    我这里google又打不开了
    -_#
      

  2.   

    楼主参考
    http://frontalboy.cnblogs.com/archive/2005/06/22/178984.html
      

  3.   

    Select 姓名=a,name,性别=a.sex,
    基本工资=sum(case when b.tzID='n1' then tzValue else 0 end),
    奖金 =sum(case when b.tzID='n2' then tzValue else 0 end),
    加班 =sum(case when b.tzID='n3' then tzValue else 0 end)
    from a inner join b on a.sortID=b.sortID
    group by a.name,a.sex
      

  4.   

    上面的还是不行select a.ID as ID,
    a.name as 姓名,
            a.sex as 性别,
    (select tzValue from B where sortID=a.sortID and tzName='基本工资') as 基本工资,
    (select tzValue from B where sortID=a.sortID and tzName='奖金') as 奖金,
    (select tzValue from B where sortID=a.sortID and tzName='加班') as 加班
    from a
    where sortid='01'
    -----------------------------------------
    select a.ID as ID,
    a.name as 姓名,
            a.sex as 性别,
    (select tzValue from B where sortID=a.sortID and tzName='基本工资') as 基本工资,
    (select tzValue from B where sortID=a.sortID and tzName='其他') as 其他
    from a
    where sortid='02'
    -----------------------------------------
    不同的条件加载不同的SQL语句,就可以了
      

  5.   

    这种情况,先读出表1的数据,以row为单位,给表2做循环,赋值给相应的column
      

  6.   

    StringBuilder _field = new StringBuilder();  //字段名
    stringBuilder _value = new StringBuilder();  //字段值........ //取表二记录集,这里就不写了SqlDataReader  dr  = Cmd.ExecuterReader();   _field.Append(dr["TZName"].ToString()+",");
       _value.Append(dr["TZValue"].ToString()+",");dr.close();.......   //关闭COMMAND,CONNECTIONstring[]  _strField = _field.split(',');  //转为数组  
    string[]  _strValue = _value.split(',');  //转为数组
    然后统一去掉数组中最后一列,因为它是空值重构DataTable  //该DATATABLE己装有表一前三列内容
    DataTable dt = new DataTable();
       for (int i = 0 ; i<_strField.Length; i++)
     {
         添加列,列名为 _strField[i].ToString();
            for (int ii = 0 ; ii<dt.Rows.Count; ii++)
          
                 {
                   为每列添加行,值为: _strValue[i].ToString();              } }
      

  7.   

    想实现动态字段就用存储过程传参数进去:
    create proc my_proc(@sortid int)
    as
    begin
    if @sortid=2
    Select  a.姓名,a.性别,
            sum(case when b.tzName='基本工资' then b.tzvalue else 0 end)[基本工资],
            sum(case when b.tzName='其他' then b.tzvalue else 0 end)[其他]
    from a inner join b on a.sortid=b.sortid and a.sortid=2
    group by a.姓名,a.性别
    else
    Select  a.姓名,a.性别,
            sum(case when b.tzName='基本工资' then b.tzvalue else 0 end)[基本工资],
            sum(case when b.tzName='奖金' then b.tzvalue else 0 end)[奖金],
            sum(case when b.tzName='加班' then b.tzvalue else 0 end)[加班]
    from a inner join b on a.sortid=b.sortid and a.sortid=1
    group by a.姓名,a.性别
    end
      

  8.   

    不好意思,刚才的写的不太对
    应该是create proc my_proc(@sortid int)
    as
    begin
    if @sortid=2
    Select  a.name,a.sex,
            sum(case when b.tzName='基本工资' then b.tzvalue else 0 end)[基本工资],
            sum(case when b.tzName='其他' then b.tzvalue else 0 end)[其他]
    from a inner join b on a.sortid=b.sortid and a.sortid=2
    group by a.name,a.sex
    else
    Select  a.name,a.sex,
            sum(case when b.tzName='基本工资' then b.tzvalue else 0 end)[基本工资],
            sum(case when b.tzName='奖金' then b.tzvalue else 0 end)[奖金],
            sum(case when b.tzName='加班' then b.tzvalue else 0 end)[加班]
    from a inner join b on a.sortid=b.sortid and a.sortid=1
    group by a.name,a.sex
    end
    ——————————————————————————————
    然后在.net里面执行存储过程,把结果绑定到datagrid中就可以了
      

  9.   

    这个题目说的不太明白 具体的看
    http://community.csdn.net/Expert/topic/4989/4989253.xml?temp=.9406397
      

  10.   

    ============================
    B表中的记录不是固定的 是可以任意增加的     ID  tzID  tzName     tzValue  sortID
        1   n1    基本工资   500      01
        2   n2    奖金       100      01
        3   n3    加班       50       01
        4   n1    基本工资   200      02
        5   n2    其他       20       02
        6   n4    加班2      50       01
        7   n5    补贴       320      01
        ...
    当 sortID=01时的datagrid显示的结果为
       姓名  性别 基本工资 奖金 加班 加班2   补贴
       张三  男   500      100  50   50      320
       李四  女   500      100  50   50      320
    =======================
      

  11.   

    declare @sql varchar(8000)
    set @sql = 'select b.name as 姓名,b.sex as 性别,b.tzID,'
    select @sql = @sql + 'sum(case xmName when '''+xmName +''' 
                              then xmdevalue else 0 end) as '''+xmName+''','
      from (select distinct xmName  from text01 where tzid=02 ) as a
    select @sql = left(@sql,len(@sql)-1) + ' from Text02 A right JOIN (select tzid,name,sex from text03) as b On A.tzID=B.tzID where a.tzid=01 group by b.tzid,b.name,b.sex'exec (@sql)把 text02 text03换成你的表名 试试