问题是这样----(关于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、问题的提出:有这样一个数据表(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语言能够实现吗?最佳的解法应该用什么方法呢?谢谢
解决方案 »
- fastreport3如何使用报表显示出来??
- 关于在DBgrid(DBgrideh)的单元格直接输入数据保存到数据库表中的问题(paradox)
- 请问这行代码是干什么用的
- Winexec的机制问题,请懂的哥哥姐姐告诉我啊。
- 如何取得默认打印机的纸张类型及其长宽
- 动态创建的VCL的事件怎么写?
- 以下录音程序有问题??
- 小小问题:更新一个字段!!
- 请问如下vb代码转换为delphi应该是什么????
- 当在easyGrid或stringGrid中选择一个单元格时,会有一个黑边框,如何去除这个边框呢?
- 运行时不能编辑DBgrid内的内容(数据源用Query设置为表的一部分)
- 新年散分送礼:程序员适用函数及Delphi程序员参考大全下载
如果已经不能改了,用存储过程解决吧,SQL Server语言可以解决你的问题,
自己多看看帮助吧,呵呵
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
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
再请问一下各位大侠特别是qizhanfeng(glacier)兄:如果不用sql,该如何写法?如果字段和数据较多的话,哪种方法处理速度快一些?先行谢过!!