【group by与distinct的区别】
如果在字段串列中没使用字段函数,则group by参数与关键字distinct的效果相同。不过使用group
by参数的效果显然要比使用关键字distinct来得快,而且在字段串列中的字段数越多,速度的差距会越大。
如果在字段串列中使用字段函数且使用group by 参数,则仍会显示出多个数据记录。但是如果于字段串列中使用字段且使用关键字distinct,则只有一个数据记录会显示出来。显然有字段时应使用group
by 参数。
例子:
1、select name,max(salary) from test grou by name
2、select dist name,max(salary) from test
结果:
grouy by distinct
name salary name salary name salary
alex 20 alex 90 mary 95
alex 10 mary 44
alex 50 tom 95
alex 90
alex 30
tom 45
tom 55
tom 15
tom 95
mary 33
mary 44
【快速排名次】
考试排名次,一般有个要求,就是如果有两个人并列第一,那跟着的就不是第二名,而是第三名,即第二名跳空了。所以我想出以下这个方法。
面对大数据量的排名次,若用scan...endscan或do while这种方法,时间会非常长。1048576个记录中花了十多分钟才排了四十多万条记录。而我这种方法只需九十多秒(硬件:PII300(100*3),128M
PC100,5.1G,VFP5.0),方法如下:
1。数据库dele:rec_id c(7) 升序;name c(10);fs n(4,1) 降序;rec_orde
n(7),文件大小为29M,索引文件16M
2。建一个视图dele1
SELECT Dele.fs, Dele.rec_orde;
FROM dele;
ORDER BY Dele.fs DESC
3。根据视图dele1建立视图dele2
SELECT DISTINCT Dele1.fs, MIN(Dele1.rec_orde) AS rec_orde;
FROM dele1;
GROUP BY Dele1.fs
4。根据视图dele2和数据库dele建立视图dele3
SELECT Dele.rec_id, Dele.rec_orde, Dele2.rec_orde, Dele.fs;
FROM dele INNER JOIN dele2 Dele2 ;
ON Dele.fs = Dele2.fs;
ORDER BY Dele2.rec_orde
准备工作做好,现在可以排序了:
以上视图是事先做好的,不算入运行时间。
use dele1
repl rec_orde with recn() all
sele 0
use dele3
repl rec_orde_a with rec_orde_b all
brow
这就是你想要的!
以上是本人的一些心得体会,欢迎大家来信切磋。
hzzck@21cn.com
OICQ;2421316
[办公软件]在sybase中插入图片、PDF、文本文件 [办公软件]安装Sybase ASE [办公软件]linux指令大全(完整篇) [办公软件]Linux新手入门常用命令大全 [办公软件]在RedHat Linux 9里安装gaim0.80 [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法 [办公软件]Linux程序员必读:中文化与GB18030标准 [办公软件]linux指令大全 [办公软件]制作Linux启动盘的四种方法 [办公软件]Linux文件系统的反删除方法