日期:2014-05-17 浏览次数:20498 次
with a(id,date_time,number)as(
select '01','2012-5-1',100 union
select '01','2012-5-5',300 union
select '01','2012-5-6',200 union
select '01','2012-6-1',200 union
select '01','2012-6-2',600 )
,b(id,date_time1)as(
select '01','2012-5-3' union
select '01','2012-5-7' union
select '01','2012-6-5')
select id,date,SUM(number) from (
select id,date=(select MIN(date_time1) from b
where a.id=b.id and a.date_time<b.date_time1),number from a
)a group by id,date
IF OBJECT_ID('tempdb..#cu1') IS NOT NULL
DROP TABLE #cu1;
IF OBJECT_ID('tempdb..#cu2') IS NOT NULL
DROP TABLE #cu2;
WITH a1 (id,date_time,number) AS
(
SELECT '01','2012-5-1',100 UNION all
SELECT '01','2012-5-5',300 UNION all
SELECT '01','2012-5-6',200 UNION all
SELECT '01','2012-6-1',200 UNION all
SELECT '01','2012-6-2',600
)
SELECT *
INTO #cu1