| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2265 人关注过本帖
标题:请大神指点如何精简下面这段代码,提高效率?
只看楼主 加入收藏
hguyy
Rank: 2
等 级:论坛游民
帖 子:56
专家分:20
注 册:2016-1-25
结帖率:83.33%
收藏
已结贴  问题点数:9 回复次数:14 
请大神指点如何精简下面这段代码,提高效率?
程序代码:
UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3702') from qx WHERE sx.xxdm='3702'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3702' AND qx.sx>=127) from qx WHERE sx.xxdm='3702'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3702'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3702' AND qx.sx>=90) from qx WHERE sx.xxdm='3702'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3702'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3702') from qx WHERE sx.xxdm='3702'



UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3703') from qx WHERE sx.xxdm='3703'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3703' AND qx.sx>=127) from qx WHERE sx.xxdm='3703'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3703'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3703' AND qx.sx>=90) from qx WHERE sx.xxdm='3703'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3703'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3703') from qx WHERE sx.xxdm='3703'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3704') from qx WHERE sx.xxdm='3704'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3704' AND qx.sx>=127) from qx WHERE sx.xxdm='3704'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3704'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3704' AND qx.sx>=90) from qx WHERE sx.xxdm='3704'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3704'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3704') from qx WHERE sx.xxdm='3704'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3705') from qx WHERE sx.xxdm='3705'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3705' AND qx.sx>=127) from qx WHERE sx.xxdm='3705'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3705'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3705' AND qx.sx>=90) from qx WHERE sx.xxdm='3705'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3705'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3705') from qx WHERE sx.xxdm='3705'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3706') from qx WHERE sx.xxdm='3706'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3706' AND qx.sx>=127) from qx WHERE sx.xxdm='3706'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3706'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3706' AND qx.sx>=90) from qx WHERE sx.xxdm='3706'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3706'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3706') from qx WHERE sx.xxdm='3706'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3707') from qx WHERE sx.xxdm='3707'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3707' AND qx.sx>=127) from qx WHERE sx.xxdm='3707'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3707'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3707' AND qx.sx>=90) from qx WHERE sx.xxdm='3707'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3707'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3707') from qx WHERE sx.xxdm='3707'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3708') from qx WHERE sx.xxdm='3708'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3708' AND qx.sx>=127) from qx WHERE sx.xxdm='3708'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3708'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3708' AND qx.sx>=90) from qx WHERE sx.xxdm='3708'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3708'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3708') from qx WHERE sx.xxdm='3708'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3709') from qx WHERE sx.xxdm='3709'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3709' AND qx.sx>=127) from qx WHERE sx.xxdm='3709'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3709'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3709' AND qx.sx>=90) from qx WHERE sx.xxdm='3709'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3709'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3709') from qx WHERE sx.xxdm='3709'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3710') from qx WHERE sx.xxdm='3710'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3710' AND qx.sx>=127) from qx WHERE sx.xxdm='3710'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3710'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3710' AND qx.sx>=90) from qx WHERE sx.xxdm='3710'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3710'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3710') from qx WHERE sx.xxdm='3710'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3711') from qx WHERE sx.xxdm='3711'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3711' AND qx.sx>=127) from qx WHERE sx.xxdm='3711'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3711'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3711' AND qx.sx>=90) from qx WHERE sx.xxdm='3711'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3711'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3711') from qx WHERE sx.xxdm='3711'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3712') from qx WHERE sx.xxdm='3712'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3712' AND qx.sx>=127) from qx WHERE sx.xxdm='3712'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3712'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3712' AND qx.sx>=90) from qx WHERE sx.xxdm='3712'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3712'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3712') from qx WHERE sx.xxdm='3712'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3713') from qx WHERE sx.xxdm='3713'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3713' AND qx.sx>=127) from qx WHERE sx.xxdm='3713'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3713'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3713' AND qx.sx>=90) from qx WHERE sx.xxdm='3713'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3713'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3713') from qx WHERE sx.xxdm='3713'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3714') from qx WHERE sx.xxdm='3714'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3714' AND qx.sx>=127) from qx WHERE sx.xxdm='3714'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3714'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3714' AND qx.sx>=90) from qx WHERE sx.xxdm='3714'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3714'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3714') from qx WHERE sx.xxdm='3714'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3715') from qx WHERE sx.xxdm='3715'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3715' AND qx.sx>=127) from qx WHERE sx.xxdm='3715'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3715'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3715' AND qx.sx>=90) from qx WHERE sx.xxdm='3715'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3715'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3715') from qx WHERE sx.xxdm='3715'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3716') from qx WHERE sx.xxdm='3716'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3716' AND qx.sx>=127) from qx WHERE sx.xxdm='3716'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3716'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3716' AND qx.sx>=90) from qx WHERE sx.xxdm='3716'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3716'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3716') from qx WHERE sx.xxdm='3716'


UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm='3717') from qx WHERE sx.xxdm='3717'
UPDATE sx SET yxrs=(select COUNT(*) from qx WHERE qx.xxdm='3717' AND qx.sx>=127) from qx WHERE sx.xxdm='3717'
UPDATE sx SET yxl=yxrs/zxss*100 WHERE xxdm='3717'
UPDATE sx SET jgrs=(select COUNT(*) from qx WHERE qx.xxdm='3717' AND qx.sx>=90) from qx WHERE sx.xxdm='3717'
UPDATE sx SET jgl=jgrs/zxss*100 WHERE xxdm='3717'
UPDATE sx SET dkzf=(select sum(sx) from qx WHERE qx.xxdm='3717') from qx WHERE sx.xxdm='3717'

UPDATE sx SET pjf=dkzf/zxss
UPDATE sx SET pjfl=dkzf/zxss
UPDATE sx SET zhb=yxl*0.3+jgl*0.4+pjfl*0.3

UPDATE sx SET pm=b.pm from (SELECT zhb,RECNO() pm FROM (SELECT distinct zhb FROM sx ORDER BY zhb DESC) a) b WHERE sx.zhb=b.zhb




这段代码是计算qx表中数学成绩情况表,我用这些代码可以执行,但好像太长了,有没有什么办法可以把这些代码精简,提高效率?qx表和数学表如下:
数据表.zip (102.96 KB)



[此贴子已经被作者于2016-6-4 09:59编辑过]

搜索更多相关主题的帖子: color 如何 
2016-06-04 09:55
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9841
专家分:27213
注 册:2012-2-5
收藏
得分:0 
贴出各项指标计算公式

坚守VFP最后的阵地
2016-06-04 10:25
mywisdom88
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:191
帖 子:3147
专家分:8408
注 册:2015-3-25
收藏
得分:0 
zxss=(select COUNT(*) from qx WHERE qx.xxdm='3702') from qx WHERE sx.xxdm='3702'
他需要更新的结果都是从汇总得到的,他汇总数据源都是从QX来的,估计可以优化。
不知道能不能先汇总后,然后1次性更新。。

2016-06-04 10:40
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9841
专家分:27213
注 册:2012-2-5
收藏
得分:4 
select xxdm from sx into cursor dm
scan
    UPDATE sx SET zxss=(select COUNT(*) from qx WHERE qx.xxdm=dm.xxdm) from qx WHERE sx.xxdm=dm.xxdm
*以此类推
endscan

坚守VFP最后的阵地
2016-06-04 12:42
aaaaaa
Rank: 8Rank: 8
等 级:贵宾
威 望:21
帖 子:796
专家分:937
注 册:2012-9-4
收藏
得分:0 
楼主你上次把问题交代清楚了,当时就有答案了。
比如:
字段名用中文标注一下,各个勾稽关系;
这次的代码也应该用中文适当的标注一下,比如:pjf/pjfl 等变量谁猜得出?
如果用一句命令的应该可以实现的,但效率不一定有多句语句的高。
我试试看,排名就是记录号,如果再单独列出排名字段,程序就复杂了,做也是可以一句命令做的,
还有,你的
pjf=dkzf/zxss
pjfl=dkzf/zxss
什么意思,公式都一样的,好像是
pjf  : 单科平均分
pjfl : 单科平均分率

===========================================================================
gnPassMark =  90  && 及格分数
gnFineMark = 127  && 优秀分数

*!*    勾稽关系:
*!*    zxss : 到考数???
*!*    yxrs : 优秀人数
*!*    yxl  : 优秀率
*!*    jgrs : 单科及格人数
*!*    jgl  : 单科及格率
*!*    dkzf : 单科总分
*!*    pjf  : 单科平均分
*!*    pjfl : 单科平均分率
*!*    zhb  : 综合比

Select Xxmc As 学校, ;
    Cnt(*) As 到考数, ;
    Sum(Iif(sx >= gnFineMark, 1, 0)) As 优秀人数, ;
    Round((Sum(Iif(sx >= gnFineMark, 1, 0)) / Cnt(*) ) * 100, 2) As 优秀率, ;
    Sum(Iif(sx >= gnPassMark, 1, 0)) As 单科及格人数, ;
    Round((Sum(Iif(sx >= gnPassMark, 1, 0)) / Cnt(*) ) * 100, 2) As 单科及格率, ;
    Sum(sx) As 单科总分, ;
    Round(Sum(sx)/Cnt(*), 2) As 单科平均分率, ;
    Round(0.3*Round((Sum(Iif(sx >= gnFineMark, 1, 0)) / Cnt(*) ) * 100, 2) + ;
    0.4*Round((Sum(Iif(sx >= gnPassMark, 1, 0)) / Cnt(*) ) * 100, 2) + ;
    0.3*Round(Sum(sx)/Cnt(*), 2), 2) As 综合比 ;
    From qx ;
    Group By Xxmc

民工子弟学校22班团小组长阳光模特队长冲锋篮球队前锋小苹果合唱队领唱蓝天舞蹈队编舞
2016-06-04 13:31
aaaaaa
Rank: 8Rank: 8
等 级:贵宾
威 望:21
帖 子:796
专家分:937
注 册:2012-9-4
收藏
得分:5 
换一种做法,一句 SQL 命令,一气呵成,包括排名:

gnPassMark =  90  && 及格分数
gnFineMark = 127  && 优秀分数

Select Xxmc As 学校, 到考数, 优秀人数, 优秀率, 单科及格人数, 单科及格率, 单科总分, ;
    单科平均分, 单科平均分率, 综合比, Recno() As 名次 ;
    From ( Select Xxmc, Cnt(*) As 到考数, ;
    Sum(Iif(sx >= gnFineMark, 1, 0)) As 优秀人数, ;
    Round((Sum(Iif(sx >= gnFineMark, 1, 0)) / Cnt(*) ) * 100, 2) As 优秀率, ;
    Sum(Iif(sx >= gnPassMark, 1, 0)) As 单科及格人数, ;
    Round((Sum(Iif(sx >= gnPassMark, 1, 0)) / Cnt(*) ) * 100, 2) As 单科及格率, ;
    Sum(sx) As 单科总分, ;
    Avg(sx) As 单科平均分, ;
    Round(Sum(sx)/Cnt(*), 2) As 单科平均分率, ;
    Round( 0.3*Sum(Iif(sx >= gnFineMark, 1, 0)) / Cnt(*) * 100 + ;
    0.4*Sum(Iif(sx >= gnPassMark, 1, 0)) / Cnt(*) * 100 + ;
    0.3*Sum(sx)/Cnt(*), 2 ) As 综合比 ;
    From Qx Group By Xxmc Order By 综合比 Desc ) T

民工子弟学校22班团小组长阳光模特队长冲锋篮球队前锋小苹果合唱队领唱蓝天舞蹈队编舞
2016-06-04 17:13
aaaaaa
Rank: 8Rank: 8
等 级:贵宾
威 望:21
帖 子:796
专家分:937
注 册:2012-9-4
收藏
得分:0 
还可以用 Join 连接的做法,但 Join 连接速度慢,不再举一反三了。

民工子弟学校22班团小组长阳光模特队长冲锋篮球队前锋小苹果合唱队领唱蓝天舞蹈队编舞
2016-06-04 17:21
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9841
专家分:27213
注 册:2012-2-5
收藏
得分:0 
排名次,要考虑到相同分数问题。

坚守VFP最后的阵地
2016-06-04 17:58
aaaaaa
Rank: 8Rank: 8
等 级:贵宾
威 望:21
帖 子:796
专家分:937
注 册:2012-9-4
收藏
得分:0 
有多种排名法,以前这里都讨论过,有示例,就不展开了,好吗?

方法 1:1224 排名法/标准竞赛排名法 (同号取小号) = Standard Competition Ranking ("1224" Ranking)
方法 2:1334 排名法/改进型竞赛排名法 (同号取大号) = Modified Competition Ranking ("1334" ranking)
方法 3:1234 排名法/序数排名法 (排名值联号) = Ordinal Ranking ("1234" Ranking)
方法 4:1223 排名法/密集排名法,(排名值联号) = Dense Ranking ("1223" Ranking)
方法 5:百分比排名法 = Percent To Total
方法 6:百分比累计排名法 = Cumulative Percent To Total
方法 7:1 2.5 2.5 4 排名法/分数排名法 = Fractional Ranking ("1 2.5 2.5 4" Ranking)

民工子弟学校22班团小组长阳光模特队长冲锋篮球队前锋小苹果合唱队领唱蓝天舞蹈队编舞
2016-06-04 20:26
hguyy
Rank: 2
等 级:论坛游民
帖 子:56
专家分:20
注 册:2016-1-25
收藏
得分:0 
回复 9楼 aaaaaa
太感谢你了
2016-06-04 21:29
快速回复:请大神指点如何精简下面这段代码,提高效率?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038346 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved