Here is one brute force way of doing it. You can probably simplify it by unpivoting the value columnsCREATE TABLE #CallsLog(
[Date] [datetime] NOT NULL,
[callsattempted] [int] NOT NULL,
[callsplaced] [int] NOT NULL,
[callscompleted] [int] NOT NULL,
[No_Response] [int] NOT NULL,
[VM] [int] NOT NULL,
[Status] [nvarchar](20) NULL
)
INSERT INTO #CallsLog VALUES
('2012-12-02','66','22','18','10','3','Forward'),
('2012-12-01','28','10','15','13','34','Forward'),
('2012-11-30','10','37','7','29','16','Complete'),
('2012-11-29','36','11','27','25','19','Complete'),
('2012-11-28','27','24','21','3','11','Complete'),
('2012-11-27','31','28','11','20','34','Complete'),
('2012-11-26','133','66','16','24','1','Complete'),
('2012-11-25','17','1','30','21','34','Forward'),
('2012-11-24','19','25','13','24','30','Forward'),
('2012-11-23','19','33','8','31','36','Complete'),
('2012-11-22','14','1','15','32','36','Complete'),
('2012-11-21','26','7','31','23','17','Complete'),
('2012-11-20','39','6','26','22','16','Complete'),
('2012-11-19','14','1','39','11','36','Complete'),
('2012-11-18','8','18','28','35','19','Complete'),
('2012-11-17','4','11','17','9','8','Complete'),
('2012-11-16','8','32','7','29','19','Complete'),
('2012-11-15','17','7','16','25','33','Complete'),
('2012-11-14','33','23','29','4','13','Forward'),
('2012-11-13','13','27','9','38','17','Complete'),
('2012-11-12','12','17','23','33','1','Complete'),
('2012-11-11','33','31','17','12','38','Complete'),
('2012-11-10','23','29','33','37','34','Complete'),
('2012-11-09','26','23','34','35','12','Forward'),
('2012-11-08','10','38','16','23','9','Complete'),
('2012-11-07','29','13','30','27','22','Complete'),
('2012-11-06','37','23','9','12','27','Forward')
SELECT
a.Date,
DATEDIFF(dd,b.caDate,a.Date) AS CaDays,
DATEDIFF(dd,c.cpDate,a.Date) AS CpDays
FROM
#CallsLog a
OUTER APPLY
(
SELECT TOP (1) x.Date AS caDate
FROM #CallsLog x
WHERE
a.[callsattempted] IN
(
x.[callsattempted]
, x.[callsplaced]
, x.[callscompleted]
, x.[No_Response]
, x.[VM]
)
AND x.Date < a.Date
ORDER BY
x.Date DESC
) b
OUTER APPLY
(
SELECT TOP (1) x.Date AS CpDate
FROM #CallsLog x
WHERE
a.[callsplaced] IN
(
x.[callsattempted]
, x.[callsplaced]
, x.[callscompleted]
, x.[No_Response]
, x.[VM]
)
AND x.Date < a.Date
ORDER BY
x.Date DESC
) c
DROP TABLE #CallsLog