问题是这样----(关于delphi的,我的系统winXP,delphi7):
1、问题的提出:有这样一个数据表(table1)
  序号 字段1 字段2     
   1     1     0
   2     2     2
   3     0     1
   4     1     3
   5     2     0
   6     2     0
   7     3     3  
要求根据table1得到table2(table2字段将按照table1的序号,逐行统计累加出字段1和字段2为0、或1、2、3的次数)如下:
序号 字段1=0  字段1=1 字段1=2 字段1=3 字段2=0 字段2=1 字段2=2 
字段2=3
 1      0         1         0        0         1      0          0       
0
 2      0         1         1        0         1      0          1       
0
 3      1         1         1        0         1      1          1       
0
 4      1         2         1        0         1      1          1       
1
 5      1         2         2        0         2      1          1       
1
 6      1         2         3        0         3      1          1       
1
 7      1         2         3        1         3      1          1       
2     
从table2的“序号7”一行可看出:字段1为0的次数1次,为1的次数有2次,为2有3次,为3有1次;
字段2为0有3次,为1有1次,为2有1次,为3有2次。
2、如果table1的字段数是变化的,能否根据table1自动创建table2,亦即在程序运行中创建table2?再请教用SQL语言能够实现吗?最佳的解法应该用什么方法呢?谢谢

解决方案 »

  1.   

    最佳解决方法是在程序Insert时另建一个表用来记录字段1的值和对应的次数。
    如果已经不能改了,用存储过程解决吧,SQL Server语言可以解决你的问题,
    自己多看看帮助吧,呵呵
      

  2.   

    感谢huanyi(残荷淡月)的回复,能针对性地帮忙Code一下吗?
      

  3.   

    --创建测试表
    create table t( 序号 int, 字段1 int, 字段2 int)
    Insert into t select    1,     1,     0
    union select   2,     2,     2
    union select    3,     0,     1
    union select   4,     1,     3
    union select   5,     2,     0
    union select   6,     2,     0
    union select   7,     3,     3 
    --select * from t order by 序号--结果
    select 序号,
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段1=0)[字段1=0],
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段1=1)[字段1=1], 
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段1=2)[字段1=2],
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段1=3)[字段1=3],
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段2=0)[字段2=0],
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段2=1)[字段2=1], 
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段2=2)[字段2=2],
    (select count(*) from t t1 where t1.序号<=t.序号 and t1.字段2=3)[字段2=3]
    from t order by 序号--删除测试表
    drop table t
      

  4.   

    create table t( 序号 int, 字段1 int, 字段2 int)
    Insert into t select    1,     1,     0
    union select   2,     2,     2
    union select    3,     0,     1
    union select   4,     1,     3
    union select   5,     2,     0
    union select   6,     2,     0
    union select   7,     3,     3 
    go
    --结果
    declare @sql varchar(8000),@i int
    select @sql = 'select 序号',@i = -1
    select @Sql= @sql+',(select count(*) from t t1 where t1.序号<=t.序号 and t1.'+[name]+'=0)['+[name]+'=0] '+char(13)
     +',(select count(*) from t t1 where t1.序号<=t.序号 and t1.'+[name]+'=1)['+[name]+'=1] '+char(13)
     +',(select count(*) from t t1 where t1.序号<=t.序号 and t1.'+[name]+'=2)['+[name]+'=2] '+char(13)
     +',(select count(*) from t t1 where t1.序号<=t.序号 and t1.'+[name]+'=3)['+[name]+'=3] '+char(13)
     from syscolumns 
    where id = object_id('t') and [name]<> '序号' 
    select @sql= @sql+'from t order by 序号'
    exec (@sql)
    go
    drop table t
      

  5.   

    qizhanfeng(glacier)兄的第一种办法好像不能满足table字段变化的情况,第二种值得一试。
    再请问一下各位大侠特别是qizhanfeng(glacier)兄:如果不用sql,该如何写法?如果字段和数据较多的话,哪种方法处理速度快一些?先行谢过!!
      

  6.   

    我觉得用sql比较简捷而且效率也很高,如果在程序里处理比较繁琐如果上面table1的0,1,2,3也是变化的需要改写我上面第二个的sql
      

  7.   

    哪位仁兄再来Code一下,明天散分!