May 1, 2013 at 10:10 am
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
May 1, 2013 at 12:59 pm
; 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
May 1, 2013 at 1:30 pm
Thank You Friend ...
Thank you so much...
Tc..
Thanks & Regards,
Saravanan.D
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply