select sum(data) as data, max(date) as date from test where date < '02/01/2015'
union all
select data,date from test
where date between '02/01/2015' and '03/01/2015'
Date :
2015-01-29 17:23:03
By :
Chaidhanan
No. 5
Guest
Code (SQL)
DECLARE @A TABLE (ID int)
INSERT INTO @A VALUES (1), (2), (3), (4), (5)
/*A. */
SELECT a.*, p.*, (SELECT SUM(ID) FROM @A x WHERE x.ID < a.ID) AS x
FROM @A a
CROSS APPLY
(
SELECT TOP 1 ID AS y FROM @A p
WHERE p.ID < a.ID ORDER BY ID DESC
) p
/*B. */
SELECT a.*, p.*, (SELECT SUM(ID) FROM @A x WHERE x.ID < a.ID) AS x
FROM @A a
OUTER APPLY
(
SELECT TOP 1 ID AS y FROM @A p
WHERE p.ID < a.ID ORDER BY ID DESC
) p