写一个Sql语句
电话明细表Phones(phone电话号码,startime开始通话时间,endtime结束时间)
月度总结表totals(phone电话号码,months月份,total总的通话时间,count通话次数)
现在从电话明细表中选择数据插入到月度总结表中
------解决方案--------------------drop table phones
create table phones(phone varchar(10),startime datetime,endtime datetime)
insert phones
select '12345678 ', '2007-01-01 08:10 ', '2007-01-01 08:25 ' union
select '12345678 ', '2007-01-01 09:10 ', '2007-01-01 09:25 ' union
select '12345678 ', '2007-01-01 10:10 ', '2007-01-01 10:25 ' union
select '12345678 ', '2007-02-02 10:10 ', '2007-02-02 10:25 ' union
select '12345678 ', '2007-02-02 14:10 ', '2007-05-02 14:25 '
select phone,months=month(startime),rtrim(sum(DATEDIFF(hour, startime, endtime)))+ '小时 '+rtrim(sum(DATEDIFF(minute, startime, endtime))),count(startime)
from Phones
group by phone,month(startime)
select phone,months=month(startime),rtrim(sum(DATEDIFF(hour, startime, endtime)*60+DATEDIFF(minute, startime, endtime)))+ '分 ',count(startime)
from Phones
group by phone,month(startime)
------解决方案----------------------try
create table Phones(phone varchar(20), startime datetime, endtime datetime)
go
create table totals(phone varchar(20), months varchar(20), total int, [count] int)
go
insert into totals
select phone, months=convert(char(7), startime, 120), total=sum(datediff(second, startime, endtime)), [count]=count(*)
from Phones
group by phone, convert(char(7), startime, 120)
--没有考虑到通话时间跨月的问题