27,580
社区成员
发帖
与我相关
我的任务
分享
DECLARE @strid VARCHAR(10)
DECLARE @sdate DATETIME
DECLARE @edate DATETIME
SET @strid ='A1'
SET @sdate = '2013-04-12 07:00:00'
SET @edate = '2013-04-20 00:00:00'
SELECT id,SUM(countA) AS scountA,CONVERT(VARCHAR(10),updatetime,120) AS updatetime
FROM (
SELECT *
FROM [F]
WHERE id =@strid AND updatetime BETWEEN @sdate AND @edate
UNION ALL
SELECT TOP 1 * FROM [F]
WHERE id =@strid AND updatetime < @sdate
ORDER BY UPDATEtime DESC)T
GROUP BY id,CONVERT(VARCHAR(10),updatetime,120)
/*
id scountA updatetime
A1 24 2013-04-12
A1 50 2013-04-18
A1 107 2013-04-19*/
create table #test (id nvarchar(4),countA int,updateTime datetime)
insert into #test
select 'A1',20,'2013-04-18 09:00:10' union all
select 'A1',30,'2013-04-18 17:00:14' union all
select 'A1',107,'2013-04-19 19:20:15' union all
select 'A1',24,'2013-04-12 06:06:50' union all
select 'A2',72,'2013-04-19 21:00:10'
SELECT ID,SUM(countA)countA ,CONVERT(VARCHAR(10),updateTime,120) FROM #test
WHERE id ='A1' AND updatetime BETWEEN '2013-04-12 06:06:50' AND '2013-04-20 00:00:00'
GROUP BY ID,CONVERT(VARCHAR(10),updateTime,120)
if object_id('[F]') is not null drop table [F]
go
create table [F] (id nvarchar(4),countA int,updateTime datetime)
insert into [F]
select 'A1',20,'2013-04-18 09:00:10' union all
select 'A1',30,'2013-04-18 17:00:14' union all
select 'A1',107,'2013-04-19 19:20:15' union all
select 'A1',24,'2013-04-12 06:06:50' union all
select 'A2',72,'2013-04-19 21:00:10'
select * from [F]
DECLARE @strid VARCHAR(10)
DECLARE @sdate DATETIME
DECLARE @edate DATETIME
SET @strid ='A1'
SET @sdate = '2013-04-12 07:00:00'
SET @edate = '2013-04-20 00:00:00'
SELECT *
FROM [F]
WHERE id =@strid AND updatetime BETWEEN @sdate AND @edate
/*
id countA updateTime
A1 20 2013-04-18 09:00:10.000
A1 30 2013-04-18 17:00:14.000
A1 107 2013-04-19 19:20:15.000*/