Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with Query

Author  Topic 

sandesh.ravi
Posting Yak Master

110 Posts

Posted - 2012-01-25 : 05:53:09
Help with query.

I have a table with columns

PAN
ProgramID
TransactDate

I need to write a stored proc that would do the following tasks.
1. The input to the stored proc is start date and end date.
2. From the start date month wise I need to find out the list of pans repeating each month and the deleted or new pans added next month and so on.

For eg.

Jan 2011 to April 2011

PAN ProgramID TransacDate
1 A 1/1/2011
2 B 2/1/2011
1 A 2/2/2011
3 C 3/2/2011
1 A 4/3/2011
3 C 4/3/2011
4 D 5/3/2011

So PAN 1 is repeating everymonth
PAN 2 is deleted in feb and PAN 3 is added in feb. and 4 is added in march

SO The output has to be
Repeated Pans, Pans deleted and Pans added. I would require count of these month wise.


Kindly throw some inputs..


Thanks,
Sandesh

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-25 : 07:07:26
Is this the same issue as what you have described in your other posting? http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=170629

People who manage this forum frown up on people posting the same issue to more than one forum. Going forward you may want to pick the forum that seems most appropriate and post only to that forum.
Go to Top of Page

sandesh.ravi
Posting Yak Master

110 Posts

Posted - 2012-01-25 : 07:25:01
Ya. Sorry for that. I would not repeat this.

Thanks,
Sandesh
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-25 : 19:50:15
can you show us how the expected result looks like ?

[edit]
Strange... i didn't see the 2 earlier replies when i open the thread.
[/edit]

KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sandesh.ravi
Posting Yak Master

110 Posts

Posted - 2012-01-26 : 04:24:56
The output:

Month Repeated Pans(count) Pans deleted(count) and Pans added(count)
Jan-2011 0 0 2
Feb-2011 1 1 1
Mar-2011 2 0 1



Thanks,
Sandesh
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-26 : 08:38:33
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?
-- TESTDATA
CREATE 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

-- QUERY
DECLARE @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.Added
FROM
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

-- CLEANUP
DROP TABLE #PaanBazaar;
GO
Go to Top of Page
   

- Advertisement -