Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Hot to Get Cumulative Total.. Expand / Collapse
Author
Message
Posted Wednesday, May 1, 2013 10:10 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, March 15, 2014 10:37 AM
Points: 46, Visits: 182
Hi Friends...

I Having Table For The Following Structure..

Create Table EmpProcessDetail ( EmpId  varchar(25), EmpName varchar(50),ProcessName varchar(30),GroupCode varchar(5),PlannedDays int,ActualDays int,
DelayedDays int,TotalDelayDays int)

insert into EmpProcessDetail values ('AKM_1','Saran','Purchase','A',15,11,4,NULL),
('AKM_1','Saran','Measurement','B',18,15,3,NULL),
('AKM_1','Saran','Cutting','C',12,15,-3,NULL),
('AKM_1','Saran','Stitching','D',20,27,-7,NULL),
('AKM_1','Saran','Ironing','E',14,19,-5,NULL),
('AKM_1','Saran','Packing','F',15,13,2,NULL),
('AKM_1','Saran','Checking','G',18,12,6,NULL),
('AKM_1','Saran','Delivery','H',22,33,-11,NULL),

('AKM_2','Arun','Purchase','A',12,13,-1,NULL),
('AKM_2','Arun','Measurement','B',15,19,-4,NULL),
('AKM_2','Arun','Cutting','C',22,29,-7,NULL),
('AKM_2','Arun','Stitching','D',41,43,-2,NULL),
('AKM_2','Arun','Ironing','E',27,26,-1,NULL),
('AKM_2','Arun','Packing','F',18,13,5,NULL),
('AKM_2','Arun','Checking','G',11,14,-3,NULL),
('AKM_2','Arun','Delivery','H',17,24,-7,NULL),

('AKM_3','Kumar','Purchase','A',41,43,-2,NULL),
('AKM_3','Kumar','Measurement','B',23,26,-3,NULL),
('AKM_3','Kumar','Cutting','C',22,29,-7,NULL),
('AKM_3','Kumar','Stitching','D',41,43,-2,NULL),
('AKM_3','Kumar','Ironing','E',12,13,-1,NULL),
('AKM_3','Kumar','Packing','F',14,19,-5,NULL),
('AKM_3','Kumar','Checking','G',12,14,-2,NULL),
('AKM_3','Kumar','Delivery','H',18,13,5,NULL)


select * from EmpProcessDetail

Below I have mentioned The my Required Table Data


EmpId                     EmpName                                            ProcessName                    GroupCode PlannedDays ActualDays  DelayedDays TotalDelayDays
------------------------- -------------------------------------------------- ------------------------------ --------- ----------- ----------- ----------- --------------
AKM_1 Saran Purchase A 15 11 4 4
AKM_1 Saran Measurement B 18 15 3 7
AKM_1 Saran Cutting C 12 15 -3 4
AKM_1 Saran Stitching D 20 27 -7 -3
AKM_1 Saran Ironing E 14 19 -5 -8
AKM_1 Saran Packing F 15 13 2 -6
AKM_1 Saran Checking G 18 12 6 0
AKM_1 Saran Delivery H 22 33 -11 -11
AKM_2 Arun Purchase A 12 13 -1 -1
AKM_2 Arun Measurement B 15 19 -4 -5
AKM_2 Arun Cutting C 22 29 -7 -12
AKM_2 Arun Stitching D 41 43 -2 -14
AKM_2 Arun Ironing E 27 26 -1 -15
AKM_2 Arun Packing F 18 13 5 -10
AKM_2 Arun Checking G 11 14 -3 -3
AKM_2 Arun Delivery H 17 24 -7 -10
AKM_3 Kumar Purchase A 41 43 -2 -2
AKM_3 Kumar Measurement B 23 26 -3 -5
AKM_3 Kumar Cutting C 22 29 -7 -12
AKM_3 Kumar Stitching D 41 43 -2 -14
AKM_3 Kumar Ironing E 12 13 -1 -15
AKM_3 Kumar Packing F 14 19 -5 -20
AKM_3 Kumar Checking G 12 14 -2 -22
AKM_3 Kumar Delivery H 18 13 5 -17



My Requirement :

1) I want to be Fill the TotalDelayDays Column..

2)The Cumulative Sum Based on the DelayedDays..

2) The Cumulative Sum Should be Calculated on EmpID once it will finished for particular EmpID
then Cumulative Sum should start from Initial for second EmpID ...

Thanks & Regards,
Saravanan.D
Post #1448517
Posted Wednesday, May 1, 2013 12:59 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 2, 2013 12:38 PM
Points: 1, Visits: 160
; WITH BASE AS
(
SELECT EmpId,
EmpName,
ProcessName,
GroupCode,
PlannedDays,
ActualDays,
DelayedDays,
rnk = DENSE_RANK() OVER (PARTITION BY EmpID ORDER BY GroupCode)
FROM EmpProcessDetail)

SELECT
EmpId,
EmpName,
ProcessName,
GroupCode,
PlannedDays,
ActualDays,
DelayedDays,
x.TotalDelayDays
FROM
BASE b
CROSS APPLY (SELECT TotalDelayDays = SUM(DelayedDays)
FROM BASE
WHERE EmpID = b.EmpID
AND rnk <= b.rnk) x

Post #1448578
Posted Wednesday, May 1, 2013 1:30 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, March 15, 2014 10:37 AM
Points: 46, Visits: 182

Thank You Friend ...
Thank you so much...
Tc..

Thanks & Regards,
Saravanan.D
Post #1448591
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse