请问如果进行查询汇总呀..请大家帮忙.100分呀. 表:tab有如下几列数据2006-1-1 上海 32006-4-12 北京 162007-12-19 上海 62006-10-8 上海 82007-2-3 天津 22007-9-2 上海 142006-2-5 天津 21我想查询出这样的结果:---按年进行排列,统计出总数来.2006 上海 112006 北京 162006 天津 212007 上海 202007 北京 02007 天津 2 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 说明,第一个字段是字符型.第二个字段是字符型,第三个字段INT型. select convert(char(4),日期列,120) 年份, 地址,sum(数量) 数量from tbgroup by convert(char(4),日期列,120),地址 select convert(nchar(4),时间,120) as 年,地区,sum(数量) as 数量 from tab group by convert(nchar(4),时间,120),地区order by convert(nchar(4),时间,120),地区 asc create table tb(日期 varchar(10),地址 varchar(10),数量 int)insert into tb select '2006-1-1' , '上海' , 3 insert into tb select '2006-4-12' , '北京' , 16 insert into tb select '2007-12-19', '上海' , 6 insert into tb select '2006-10-8' , '上海' , 8 insert into tb select '2007-2-3' , '天津' , 2 insert into tb select '2007-9-2' , '上海' , 14 insert into tb select '2006-2-5' , '天津' , 21 goselect * into #from (select distinct left(日期,4) 日期 from tb) a cross join (select distinct 地址 from tb) bselect a.日期,a.地址,sum(isnull(b.数量,0)) 数量from # a left outer join tb b on a.日期=left(b.日期,4) and a.地址=b.地址group by a.日期,a.地址order by a.日期godrop table tb,#/*日期 地址 数量 -------- ---------- ----------- 2006 北京 162006 上海 112006 天津 212007 北京 02007 上海 202007 天津 2(所影响的行数为 6 行)*/ select year(时间) 年份 ,省份,sum(val) val from tb group by year(时间),省份 楼上的.潇洒老乌龟(爱新觉罗.毓华),,真是非常SORRY..已经结贴了噢,,以后早点来..呵呵.. 帮忙看一个SQL语句 急急!!!!!!!!!!!! 时间类型转换,2小时内结贴 在SQLserver里面写fp tree算法,运算会比在C#里面写快吗? set和declare有什么区别 求一思路或者sql语句 我是菜鸟,请大虾多指点! 难题:!!SQL语句中:select * from tablename 其中tablename能否用变量实现? bcp错在那里? 200万条纪录,应怎样处理??? 往数据库中插入图片遇到的问题,在线等,急 请问如何 sql 区分移动联通号码
地址,sum(数量) 数量
from tb
group by convert(char(4),日期列,120),地址
group by convert(nchar(4),时间,120),地区
order by convert(nchar(4),时间,120),地区 asc
insert into tb select '2006-1-1' , '上海' , 3
insert into tb select '2006-4-12' , '北京' , 16
insert into tb select '2007-12-19', '上海' , 6
insert into tb select '2006-10-8' , '上海' , 8
insert into tb select '2007-2-3' , '天津' , 2
insert into tb select '2007-9-2' , '上海' , 14
insert into tb select '2006-2-5' , '天津' , 21
go
select * into #
from
(select distinct left(日期,4) 日期 from tb) a
cross join
(select distinct 地址 from tb) bselect a.日期,a.地址,sum(isnull(b.数量,0)) 数量
from # a left outer join tb b
on a.日期=left(b.日期,4) and a.地址=b.地址
group by a.日期,a.地址
order by a.日期go
drop table tb,#/*
日期 地址 数量
-------- ---------- -----------
2006 北京 16
2006 上海 11
2006 天津 21
2007 北京 0
2007 上海 20
2007 天津 2(所影响的行数为 6 行)
*/
select year(时间) 年份 ,省份,sum(val) val from tb group by year(时间),省份