救命啊~~~~~~~~~~~~~~大虾们帮帮忙~~~~~~~~~~~~~不胜感激涉及三个表
-----------------------
成绩表:
ID,ManID,Score
1  10    10
2  11    20
3  14    40
4  10    10
5  10    10
------------------------
地区表:
DID,
1  ...
2  ...
3  ...
----------------------
人员资料表:
ManID,DID
10     1
11     2
14     3
--------------------------------
怎么写SQL语句得到这样的结果?名次 DID TotalScore
1    x    m
2    y    n
3    z    k
.....

解决方案 »

  1.   

    select Did,sum(Score) as TotalScore from 成績表 as a left join 人員資料表 as b
    on a.manid=b.manid
      

  2.   

    什么数据库?
    TotalScore是哪个表里的?
      

  3.   

    select b.did,sum(a.Score) TotalScore from 成绩表 a,人员资料表 b where a.ManID=b.ManID group by b.did order by sum(a.Score) desc
      

  4.   

    select Did,sum(Score) as TotalScore 
    from 成績表 as a 
    left join 人員資料表 as b on a.manid=b.manid
    group by did
      

  5.   

    sqlserver:select identity(int,1,1) 名次,b.did,sum(a.Score) TotalScore into # from 成绩表 a,人员资料表 b where a.ManID=b.ManID group by b.did order by sum(a.Score) desc
    select * from #
      

  6.   

    SQL Server
    TotalScore就是总分啊谢谢suton(suton)!不过有个问题,要是某个地区(比如DID=4),属于这个地区的人员如果在成绩表中没有得分的话,在统计的名次中不会有DID=4这个地区的排名啊
      

  7.   

    你可以在table中自己增加个查询的字段阿,关联一下表就可以了
    就是双击表用add new fields 中的lookup 就可以了,至于能不能用sql一下子选出来,偶没有试过
    学习~~~
      

  8.   

    select identity(int,1,1) 名次,b.did,sum(a.Score) TotalScore into # from 成绩表 a,人员资料表 b where a.ManID=b.ManID group by b.did order by sum(a.Score) desc
    select * from #
      

  9.   

    select identity(int,1,1) 名次,b.did,isnull(sum(a.Score),0) TotalScore into # from 成绩表 a right join 人员资料表 b on a.ManID=b.ManID group by b.did order by isnull(sum(a.Score),0) desc
    select * from #
      

  10.   

    要是某个地区(比如DID=4),属于这个地区的人员如果在成绩表中没有得分的话,在统计的名次中不会有DID=4这个地区的排名啊用isnull函數
      

  11.   

    A:
    *****************
    SQL Server
    TotalScore就是总分啊谢谢suton(suton)!不过有个问题,要是某个地区(比如DID=4),属于这个地区的人员如果在成绩表中没有得分的话,在统计的名次中不会有DID=4这个地区的排名啊
    *********************************************
    Q:
    select a.Did,sum(case when Score is null then 0 else Score end) as TotalScore 
    from 地区表 a
    left join 人員資料表 as b on a.Did=b.Didleft join as c 
    left join 成绩表     as c on  c.manid=b.manid
    group by did
      

  12.   

    thx  pengdali(大力 V2.0)和各位大虾,马上给分名次 DID TotalScore
    1    x    80
    2    y    90
    3    z    100
          总计:270  这个总计呢?
      

  13.   

    (select clause 1)  union (對clause 1 匯總的 sql 語句)