Hello Guys,
I would like to thank you for the wonderful job you are doing.
I need help my query. I run a report every day to count the total numbers of survey that come into the system. What I want to do is to get the count of today(current day) and the count of Yesterday total from my query and get the difference if any. I have included some same data, hopefully this should help. I am using SQL 2102
My desire output should be like:
Category Today_Reporting_Total Previous_Day_Reporting_Total Total_Difference
Survey1 10 12 2
DECLARE @Prev_Current TABLE
(
Category Varchar(255),
ContactID int,
EventDateTime Datetime
)
INSERT INTO @Prev_Current (Category, ContactID,EventDateTime)
VALUES
('Survey1', 245211, '2019-04-26 16:41:08.497'),
('Survey1', 245847, '2019-03-23 10:09:35.390'),
('Survey1', 245914, '2019-03-31 21:47:47.070'),
('Survey1', 246332, '2019-04-15 13:43:02.657'),
('Survey1', 246333, '2019-04-15 13:50:01.473'),
('Survey1', 245212, '2019-07-26 11:53:48.520'),
('Survey1', 245849, '2019-07-27 12:53:48.520'),
('Survey1', 245918, '2019-07-27 10:53:48.520'),
('Survey1', 246330, '2019-07-26 14:53:48.520'),
('Survey1', 246331, '2019-07-26 15:53:48.520');
Select c.Category
,Count(c.ContactID) AS Today_Reporting_Total
,Count(p.ContactID) AS Previous_Day_Reporting_Total
,Count(c.ContactID) - Count(p.ContactID) as Total_Difference
from @Prev_Current AS C
left outer join @Prev_Current AS P on c.ContactID = p.ContactID
--AND P.EventDateTime= DATEADD(d,-1,c.EventDateTime)
Group by c.Category
Thanking you in advance.