环境:delphi7 adoquery access2003
一个表cjtt中,有以下数据ID    总量    名次
1     100     
2     108     
3     93      
4     100     
5     80      
6     80      
7     79      ---------------------
如何才能得到下面的结果:ID    总量    名次
1     100     2
2     108     1
3     93      3
4     100     2
5     80      4
6     80      4
7     79      5

解决方案 »

  1.   

    update a
    set 名次 = (select count(1) from cjtt where 总量 >= a.总量)
    from cjtt as a
      

  2.   

    select a.*,(select count(1) from tb b where a.id=b.id and b.总量<=a.总量) 名次
    from tb a
      

  3.   

    select id,总量,名次=(select count(1) from cjtt where 总量<a.总量 )+1 from cjtt a
      

  4.   

    SELECT 
        ID,
        总量,
        SCORE,
        (SELECT COUNT(*) FROM cjtt WHERE 总量>A.总量)+1 AS 名次
    FROM cjtt AS A
      

  5.   

    SQL SERVER的语法是这样,ACCESS不太清楚。
      

  6.   


    --modify
    select id,总量,名次=(select count(1) from cjtt where 总量>a.总量 )+1 from cjtt a
      

  7.   

    ------------------------------------------------------------------------
    -- Author:  happyflystone  
    -- Date  :  2009-03-21 14:16:19
    -- Ver:     Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) 
    --       Apr 14 2006 01:12:25 
    --       Copyright (c) 1988-2005 Microsoft Corporation
    --       Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
    --      
    -------------------------------------------------------------------------- Test Data: ta
    IF OBJECT_ID('ta') IS NOT NULL 
        DROP TABLE ta
    Go
    CREATE TABLE ta(ID INT,总量 INT)
    Go
    INSERT INTO ta
    SELECT 1,100 UNION ALL
    SELECT 2,108 UNION ALL
    SELECT 3,93 UNION ALL
    SELECT 4,100 UNION ALL
    SELECT 5,80 UNION ALL
    SELECT 6,80 UNION ALL
    SELECT 7,79 
    GO
    alter table ta add mc int
    go
    --Startupdate a
    set mc = (select count(distinct 总量) from ta where 总量 >= a.总量)
    from ta as aSELECT 

    FROM
    TA--Result:
    /*
    ID          总量          mc
    ----------- ----------- -----------
    1           100         2
    2           108         1
    3           93          3
    4           100         2
    5           80          4
    6           80          4
    7           79          5*/
    --End 
      

  8.   

    create table #tb(id int,num numeric(12),serial numeric(5))
    insert into #tb (id,num)
    select '1','100'
    union all select '2','108'
    union all select '3','93'
    union all select '4','100'
    union all select '5','80'
    union all select '6','80'
    union all select '7','79'update #tb  set serial=(select count(1) from #tb b where  b.num>=#tb.num)select * from #tbid num serial
    ------------------------
    1 100 3
    2 108 1
    3 93 4
    4 100 3
    5 80 6
    6 80 6
    7 79 7
      

  9.   

    继续顶,在ACCESS中以上语句不能成功,请高手再试
      

  10.   

    请爱新觉罗.毓华(dawugui)出山.问题测试: Sql = " Update cjtt set 名次 =(select count(总量) AS 总量 from (select distinct 总量 from cjtt) b where b.总量>=总量) "     Sql = "Update cjtt  set 名次=(select (select count(总量) from (select distinct 总量 from cjtt) b where b.总量>=a.总量)as 名次,id from cjtt a) " 
        
        Sql = " Update cjtt inner join (select ID,(select count(总量)  from " _ 
            & " (select distinct 总量 from cjtt) b where b.总量>=a.总量 ) as 名次 " _ 
            & " from cjtt a ) d on cjtt.id=d.id set cjttaa.名次=d.名次 " 
    方法用了很多,可就是没用 
    老是提示 。。操作必须使用一个可更新的查询,难道需要用域函数?用select 语句很容易就出来了,可关键是在ACCESS中用UPDATE (而且在ACCESS中用UPDATE不能用嵌套,会出现操作必须使用一个可更新的查询)说白了ACCESS中用UPDATE就是不支持嵌套.   用 域函数 只会简单的.求助大版主出手.
      

  11.   

    这个论坛高手很多哦,谢谢各位高手的热心帮助啦。
    我的“总量”字段内的数据是用adoquery计算得来的,在内存里面,因为“总量”的数据随时都在变化着,为了提高效率,就没有真正写入Access,就是说Access中只有这个字段名,并没有数据。现在是排得的名次也不用写入Access,就在临时表中显示出来就是了。
      我阅读了很多资料,也在adoquery中写了很多语句,都没有排出来,郁闷啊。或者明天我把我写的这个程序源代码带来后发上来,请高手再帮我看看吧,这样也许更能准确的解决问题。
      
      

  12.   

    update 成绩表 a set 名次=dcount('总分','成绩表','总分>' & a.总分) +1   ------VBA中测试通过