SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Hot to Get Cumulative Total..


Hot to Get Cumulative Total..

Author
Message
sarwaanmca
sarwaanmca
SSC-Enthusiastic
SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)

Group: General Forum Members
Points: 108 Visits: 193
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
wing 54040
wing 54040
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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


sarwaanmca
sarwaanmca
SSC-Enthusiastic
SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)

Group: General Forum Members
Points: 108 Visits: 193
Thank You Friend ...
Thank you so much...
Tc..

Thanks & Regards,
Saravanan.D
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search