select 出发地 , 目的地 , sum(金钱) from tb group by 出发地 , 目的地

解决方案 »

  1.   

    create table tb(ID int, 出发地 varchar(10), 目的地 varchar(10), 金钱 int)
    insert into tb values(1 , '广州' , '佛山' , 1200 )
    insert into tb values(2 , '广州' , '佛山' , 1200 )
    insert into tb values(2 , '广州' , '中山' , 1500 )
    insert into tb values(2 , '广州' , '中山' , 1500 )
    insert into tb values(2 , '广州' , '中山' , 1500 )
    insert into tb values(2 , '广州' , '东莞' , 1000 )
    insert into tb values(2 , '广州' , '东莞' , 1000 )
    insert into tb values(2 , '广州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '中山' , 1000 )
    insert into tb values(2 , '惠州' , '中山' , 1000 )
    insert into tb values(2 , '惠州' , '中山' , 1000 )select 出发地 , 目的地 , sum(金钱) from tb group by 出发地 , 目的地 order by 出发地 , 目的地drop table tb/*
    出发地        目的地                    
    ---------- ---------- ----------- 
    广州         东莞         3000
    广州         佛山         2400
    广州         中山         4500
    惠州         东莞         3000
    惠州         中山         3000(所影响的行数为 5 行)
    */
      

  2.   

    这样?
    use tempdb
    go
    create table tb(ID int, 出发地 varchar(10), 目的地 varchar(10), 金钱 int)
    insert into tb values(1 , '广州' , '佛山' , 1200 )
    insert into tb values(2 , '广州' , '佛山' , 1200 )
    insert into tb values(2 , '广州' , '中山' , 1500 )
    insert into tb values(2 , '广州' , '中山' , 1500 )
    insert into tb values(2 , '广州' , '中山' , 1500 )
    insert into tb values(2 , '广州' , '东莞' , 1000 )
    insert into tb values(2 , '广州' , '东莞' , 1000 )
    insert into tb values(2 , '广州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '东莞' , 1000 )
    insert into tb values(2 , '惠州' , '中山' , 1000 )
    insert into tb values(2 , '惠州' , '中山' , 1000 )
    insert into tb values(2 , '惠州' , '中山' , 1000 )go
    select 
    出发地,目的地,金钱
    from 
    (select
    出发地='',目的地='合计',sum(金钱)金钱,Falg=1,show='合计',ord=出发地+'|'+目的地
    from tb
    group by 出发地,目的地
    union all 
    select
    出发地,目的地,金钱,Falg=0,show='',ord=出发地+'|'+目的地
    from tb)t
    order by ord,Falg出发地        目的地        金钱
    ---------- ---------- -----------
    广州         东莞         1000
    广州         东莞         1000
    广州         东莞         1000
               合计         3000
    广州         佛山         1200
    广州         佛山         1200
               合计         2400
    广州         中山         1500
    广州         中山         1500
    广州         中山         1500
               合计         4500
    惠州         东莞         1000
    惠州         东莞         1000
    惠州         东莞         1000
               合计         3000
    惠州         中山         1000
    惠州         中山         1000
    惠州         中山         1000
               合计         3000(19 行受影响)
      

  3.   


    create table #J
    (
    id int,
    StartTarget nvarchar(20),
    EndTarget nvarchar(20),
    money1 int
    )
    insert into #J
    select 1 ,   '广州 ',  '佛山  ' , 1200  union all
    select 2 ,   '广州' ,   '佛山 ' ,  1200 union all
    select 2 ,   '广州' ,  '中山 ' ,  1500 union all
    select 2 ,   '广州' , '中山  ' , 1500 union all
    select 2 ,   '广州'  ,  '中山'  ,  1500 union all
    select 2  ,  '广州 ' ,  '东莞 ' ,  1000 union all
    select 2 ,   '广州 ' ,  '东莞 ' , 1000 union all
    select 2  ,  '广州 ' ,  '东莞 ' ,1000 union all
    select 2  ,  '惠州 ' ,  '东莞 '  , 1000 union all
    select 2 ,   '惠州 ' ,  '东莞 '  , 1000 union all
    select 2 ,   '惠州 ' ,  '东莞 '   ,1000 union all
    select 2 ,   '惠州 ' ,  '中山 '   ,  1000 union all
    select 2 ,   '惠州 ' , ' 中山 '   ,  1000 union all
    select 2 ,   '惠州 ' ,  '中山  '   , 1000 
    select  StartTarget,EndTarget,SUM(money1) from #J group by StartTarget,EndTarget
    drop table #J
      

  4.   


    select 出发地 , 目的地 , sum(金钱) from table group by 出发地 , 目的地 order by 出发地 , 目的地
      

  5.   

    根据select 出发地 , 目的地 , sum(金钱) from tb group by 出发地 , 目的地 order by 出发地 , 目的地我改成这样select bcityid , tcityid , sum(carorigin_softprice) as go from carorigin group by bcityid ,tcityid
    出发地   sum(金钱)目的地
    341300 7000 320100
    360300 6500 420100
    430100 6000 130200
    430900 4000 460200
    440600 8000 370100
    445200 1200 320100
    450800 4000 460200
    450800 7000 371300
    510100 6000 460200
    520200 9000 320100
    542100 7500 360200
    542100 7000 530500
    542200 3333 320100
    610200 21500 320100
    621200 6000 520200
    621200 7000 542100
    621200 1000 610200
    632100 888 460200但是结果出来是错的
      

  6.   

    我改成这样
        select bcityid , tcityid , sum(carorigin_softprice) as [go]--加上[]
     from carorigin group by bcityid ,tcityid
      

  7.   

    --> 测试数据: @s
    declare @s table (ID int,出发地 varchar(4),目的地 varchar(4),金钱 int)
    insert into @s
    select 1,'广州','佛山',1200 union all
    select 2,'广州','佛山',1200 union all
    select 2,'广州','中山',1500 union all
    select 2,'广州','中山',1500 union all
    select 2,'广州','中山',1500 union all
    select 2,'广州','东莞',1000 union all
    select 2,'广州','东莞',1000 union all
    select 2,'广州','东莞',1000 union all
    select 2,'惠州','东莞',1000 union all
    select 2,'惠州','东莞',1000 union all
    select 2,'惠州','东莞',1000 union all
    select 2,'惠州','中山',1000 union all
    select 2,'惠州','中山',1000 union all
    select 2,'惠州','中山',1000select 出发地=isnull(出发地,'总计'),目的地=isnull(目的地,'合计')
    ,金钱=sum(金钱) from @s group by 出发地,目的地
    with rollup
    --结果;
    出发地  目的地  金钱
    ---- ---- -----------
    广州   东莞   3000
    广州   佛山   2400
    广州   中山   4500
    广州   合计   9900
    惠州   东莞   3000
    惠州   中山   3000
    惠州   合计   6000
    总计   合计   15900