select 出发地 , 目的地 , sum(金钱) from tb group by 出发地 , 目的地
解决方案 »
- WIN7安装SQLSERVER的问题
- 求助 最近想写一个论坛的数据库 但是不知道用哪种方法好 给点建议
- 一个SQL语句的高难度问题,请SQL高手赐教!谢谢!
- sql中 varchar中数字转换
- 如何使sql server允许远程连接?
- 关于取多个帐期通话次数为0的数据
- SQL2000又发生置疑
- access建立链接表,默认是绝对路径,如何使用相对路径?(20分)
- SQL Server7.0和SQL Server2000的客户端安装问题?
- 关于sql server一个问题???
- 推荐系统的原理是什么呢?实现的大致思路是什么?
- 如何查看SQL server2000 的版本信息,我安装了sp4补丁(Microsoft SQL Server 2000 - 8.00.194)后1433端口还是没开
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 行)
*/
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 行受影响)
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
select 出发地 , 目的地 , sum(金钱) from table group by 出发地 , 目的地 order by 出发地 , 目的地
出发地 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但是结果出来是错的
select bcityid , tcityid , sum(carorigin_softprice) as [go]--加上[]
from carorigin group by bcityid ,tcityid
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