现在有2个表:
表一:ID         Money
      100121     12
      100131     23
      100211     14
      100311     45
表二:CountId   CountMoney
      1001      35=(12+23)
      1002      14=(14)
      1003      45=(45)
  现在要从表一中得到表二的数据用SQL语句
  希望告知!!

解决方案 »

  1.   

    select left(id,4) countid,sum([money]) countmoney
    from tb1
    group by left(id,4)
      

  2.   

    --测试数据
    create table tb(ID varchar(10),Money int)
    insert into tb select '100121',12
    insert into tb select '100131',23
    insert into tb select '100211',14
    insert into tb select '100311',45
    go
    select left(id,4)Countid,sum(money)countMoney from tb group by left(id,4)
    /*
    Countid  countMoney
    -------- -----------
    1001     35
    1002     14
    1003     45(3 行受影响)*/
    go
    drop table tb
      

  3.   


    select left(id,4) countid,sum([money]) countmoney
    from 表一
    group by left(id,4)