lz说的有点晕乎。
看看这个如何》?
--create table
DECLARE @t table(id int,Phone varchar(11),dte smalldatetime,cost int)
--prepare data
insert into @t select 1,'13146653802',GETDATE(),132
UNION ALL SELECT 2,'13146653803',DATEADD(hour,1,GETDATE()),1423
UNION ALL SELECT 3,'13146653804',DATEADD(hour,2,GETDATE()),41
UNION ALL SELECT 4,'13146653805',DATEADD(hour,3,GETDATE()),412
UNION ALL SELECT 5,'13146653806',DATEADD(day,1,GETDATE()),543
UNION ALL SELECT 6,'13146653807',DATEADD(day,1,GETDATE()),64
--display all data
select * from @t
--split dte
SELECT * ,Year(dte) as y ,Month(dte) as m,Day(dte) as d FROM @t
--group by dte's item
SELECT a.y,a.m,a.d,SUM(a.cost) as cst
FROM
(SELECT * ,Year(dte) as y ,Month(dte) as m,Day(dte) as d FROM @t) a
GROUP BY a.y,a.m,a.d
--select data between 5 and 6
SELECT * FROM
(
SELECT a.y,a.m,a.d,SUM(a.cost) as cst
FROM
(SELECT * ,Year(dte) as y ,Month(dte) as m,Day(dte) as d FROM @t) a
GROUP BY a.y,a.m,a.d
)b
where b.d between 5 and 6