This sounds like a relatively simple problem compared to what people routinely post solutions for, but when I started writing the query, it looks more complicated than it should be. So hopefully, someone will post a more compact and better solution - in which case, please do throw out mine.I am including a set-up section that sets up your data in case someone else wants to use it. (One of the hardest things most time-consuming aspects of responding to posting on this forum is setting up the test data and tables. In the future, if you post something like the section TESTDATA in my query below, that makes it easier for people to write a query against it and test it, and you will get faster and better responses).So here is my solution. Did I mention that I specialize in over-complicating solutions?
-- TESTDATACREATE TABLE #PaanBazaar( PAN INT, ProgramId VARCHAR(32), TrasactDate DATETIME);INSERT INTO #PaanBazaar VALUES('1','A','20110101'),('2','B','20110102'),('1','A','20110202'),('3','C','20110203'),('1','A','20110304'),('3','C','20110304'),('4','D','20110305');GO-- QUERYDECLARE @startDate DATE; SET @startDate = '20110101';DECLARE @endDate DATE; SET @endDate = '20111201';;WITH calendar AS( SELECT @startDate AS Dt UNION ALL SELECT DATEADD(MONTH,1,Dt) FROM calendar WHERE Dt < @endDate)SELECT c.Dt, r.Repeated, d.Deleted, a.AddedFROM calendar c OUTER APPLY ( SELECT COUNT(*) AS Repeated FROM #PaanBazaar p1 WHERE c.Dt = DATEADD(month,DATEDIFF(month,0,p1.TrasactDate),0) AND EXISTS (SELECT * FROM #PaanBazaar p2 WHERE p1.PAN = p2.PAN AND c.Dt=DATEADD(month,DATEDIFF(month,0,p2.TrasactDate)+1,0)) ) r OUTER APPLY ( SELECT COUNT(*) AS Deleted FROM #PaanBazaar p1 WHERE c.Dt = DATEADD(month,DATEDIFF(month,0,p1.TrasactDate)+1,0) AND NOT EXISTS (SELECT * FROM #PaanBazaar p2 WHERE p1.PAN = p2.PAN AND DATEADD(month,DATEDIFF(month,0,p2.TrasactDate),0)=c.Dt) ) d OUTER APPLY ( SELECT COUNT(*) AS Added FROM #PaanBazaar p1 WHERE c.Dt = DATEADD(month,DATEDIFF(month,0,p1.TrasactDate),0) AND NOT EXISTS (SELECT * FROM #PaanBazaar p2 WHERE p1.PAN = p2.PAN AND c.Dt=DATEADD(month,DATEDIFF(month,0,p2.TrasactDate)+1,0)) ) a;GO-- CLEANUPDROP TABLE #PaanBazaar;GO