表A FID  name  sex 
1    小红  女 
2    小刚  男 
3    小刘  女 
4    小明  男 表B 
ID  FID    Score  b1 b2
1    1      80     x  y
2    2      70     d  a
3    3      90     e  e
两表通过FID关联 现在我检索表B中 所有人的得分,重复的人名,分数累加 如下 FID  name  sex  Score               b1   b2
1    小红  女    140(60+80得到的)   x    y
2    小刚  男    120(70+50)         d    a
3    小刘  女    90                 e    e   
 这个sql语句应该如何写呢

解决方案 »

  1.   

    select
        A.FID,
        A.name,
        A.sex,
        sum(B.Score) as Score,
        B.B1,
        B.B2
    from
        A,B 
    where 
        A.FID=B.FID 
    group by 
        A.FID,A.name,A.sex,B.B1,B.B2 
      

  2.   


    select a.*,b.score,b1,b3
    from ta a,
    (select fid,sum(score) as score ,b1,b2 from tb group by fid,b1,b2) b
    where a.id = b.fid
      

  3.   

    ---你的数据或结果有问题
    if object_id('tb')is not null drop table tb
    go
    create table tb(FID int, name varchar(9), sex varchar(9))
    insert tb select
    1,    '小红' , '女' union all select 
    2,    '小刚' , '男' union all select 
    3,    '小刘' , '女'  union all select
    4,    '小明',  '男' if object_id('tb1')is not null drop table tb1
    go
    create table tb1( ID int, fid int, score int,b1 varchar(1),b2 varchar(1))
    insert tb1 select
    1,    1  ,    80 ,'x','y' union all select
    2,    2  ,    70 ,'d','a' union all select
    3,    3  ,    90 ,'e','e'
     select a.FID,a.name,a.sex,Score=sum(score),b1,b2
    from tb a
    right join tb1 b
    on a.fid=b.fid
    group by  a.FID,a.name,a.sex,b1,b2
    FID         name      sex       Score       b1   b2
    ----------- --------- --------- ----------- ---- ----
    1           小红        女         80          x    y
    2           小刚        男         70          d    a
    3           小刘        女         90          e    e(3 行受影响)
      

  4.   

    表B 
    ID  FID    Score 
    1    1      80 
    2    2      70 
    3    3      90 
    5    1      60 
    6    2      50 
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-10-19 16:41:18
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    go 
    create table [A]([FID] int,[name] varchar(4),[sex] varchar(2))
    insert [A]
    select 1,'小红','女' union all
    select 2,'小刚','男' union all
    select 3,'小刘','女' union all
    select 4,'小明','男'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([ID] int,[FID] int,[Score] int,[b1] varchar(1),[b2] varchar(1))
    insert [B]
    select 1,1,80,'x','y' union all
    select 2,2,70,'d','a' union all
    select 3,3,90,'e','e'
    --------------开始查询--------------------------
    select 
      a.*,b.score,b.b1,b.b2
    from 
      a, 
      (select fid,sum(score) as score ,b1,b2 from b group by fid,b1,b2) b 
    where 
       a.fid = b.fid
    ----------------结果----------------------------
    /* FID         name sex  score       b1   b2
    ----------- ---- ---- ----------- ---- ----
    1           小红   女    80          x    y
    2           小刚   男    70          d    a
    3           小刘   女    90          e    e(3 行受影响)
    */
      

  6.   

    ---就是这个就行了
    select 
      a.*,b.score,b.b1,b.b2
    from 
      a, 
      (select fid,sum(score) as score ,b1,b2 from b group by fid,b1,b2) b 
    where 
       a.fid = b.fid
      

  7.   

    create table [A]([FID] int,[name] varchar(4),[sex] varchar(2))
    insert [A]
    select 1,'小红','女' union all
    select 2,'小刚','男' union all
    select 3,'小刘','女' union all
    select 4,'小明','男'
    create table [B]([ID] int,[FID] int,[Score] int,[b1] varchar(1),[b2] varchar(1))
    insert [B]
    select 1,1,80,'x','y' union all
    select 2,2,70,'d','a' union all
    select 3,3,90,'e','e' union all
    select 5,1,60,'x','y' union all
    select 6,2,50,'d','a'select a.* , sum(b.score) score, max(b.b1) b1 , max(b.b2) b2 
    from a , b
    where a.fid = b.fid
    group by a.fid , a.name , a.sexdrop table a , b/*
    FID         name sex  score       b1   b2   
    ----------- ---- ---- ----------- ---- ---- 
    1           小红   女    140         x    y
    2           小刚   男    120         d    a
    3           小刘   女    90          e    e(所影响的行数为 3 行)*/
      

  8.   

    select a.FID,a.name,a.Sex,c.Score,b.b1,b.b2 
    from A a ,B b,(select FID,sum(Score) Score from B group by FID) c
    where a.FID=b.FID and a.FID=c.FID
      

  9.   

    2楼没有问题
    4楼的没看太懂哦
    对mssql不太用
      

  10.   

    sorry
    4楼没问题
    刚没看清楚