If wishes to find the Cumulative Values in SQL Server 2005, 2008, 2012 then make a self join of the table and achieve it. See the below example :
Reference: Govind Badkur(http://sqlserver20.blogspot.com)
CREATE TABLE UserTable (UserID VARCHAR(20), Amount DECIMAL(18,2), CreatedDate DATE)
INSERT INTO UserTable (UserID , Amount , CreatedDate )
SELECT 'U001', 20.00, '2016-08-01'
UNION ALL
SELECT 'U001', 40.00, '2016-08-02'
UNION ALL
SELECT 'U001', 30.00, '2016-08-03'
UNION ALL
SELECT 'U002', 10.00, '2016-08-01'
UNION ALL
SELECT 'U002', 20.00, '2016-08-02'
SELECT * FROM UserTable
SELECT T1.UserID, MAX(T1.Amount) Amount, SUM(T2.Amount) CummAmount, T1.CreatedDate
FROM UserTable T1 (NOLOCK)
,UserTable T2 (NOLOCK)
WHERE T1.CreatedDate >= T2.CreatedDate
AND T1.UserID = T2.UserID
GROUP BY T1.UserID,T1.CreatedDate
ORDER BY T1.UserID, T1.CreatedDate
Reference: Govind Badkur(http://sqlserver20.blogspot.com)
No comments:
Post a Comment