fetch talker rc sp's

Download Fetch Talker Rc Sp's

If you can't read please download the document

Upload: raheel

Post on 29-Sep-2015

221 views

Category:

Documents


4 download

DESCRIPTION

x

TRANSCRIPT

--NewDECLARE @CompanyId int= 111;DECLARE @KeywordId int= 289;DECLARE @Status varchar(45)='Approved';DECLARE @FromDate datetime= '2/1/2015 12:00:00 AM';DECLARE @ToDate datetime= '4/1/2015 11:59:59 PM';DECLARE @KeywordListId int = -1;DECLARE @Telco int = -1;DECLARE @SearchQuery varchar(100) = 'Zehra'DECLARE @PageNum int = 1;DECLARE @PageSize int = 50;DECLARE @AuthLists varchar(3000) = '-1' ;DECLARE @QuietStart datetime = null ;DECLARE @QuietEnd datetime = null; -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; --PREVENTING PARAMETER SNIFFING DECLARE @StartDate DATETIME; DECLARE @EndDate DATETIME; SET @StartDate = @FromDate; SET @EndDate = @ToDate; DECLARE @SubscribersByTelco TABLE ( SubscriberID bigint ) DECLARE @SelectedSubscribers TABLE ( SubscriberID bigint, SubscriptionID bigint, CellNumber varchar(20), --ImageUrl varchar(512), --ProfileUrl varchar(512), --DataCardUrl varchar(512), UpdatedOn DateTime, --FriendlyDate varchar(100), Row_Num int ) DECLARE @FinalRows TABLE ( SubscriberID bigint, SubscriptionID bigint, Name nvarchar(200), CellNumber varchar(20), ImageUrl varchar(512), ProfileUrl varchar(512), DataCardUrl varchar(512), UpdatedOn DateTime, FriendlyDate varchar(100), Row_Num int ) DECLARE @ListSubscribers TABLE ( SubscriberID bigint ) DECLARE @TextSearchSubscribers TABLE ( SubscriberID bigint ) DECLARE @AuthorisedListSubscribers TABLE ( SubscriberId bigint ) DECLARE @QuietSubscribers TABLE ( SubscriberId bigint ) --FETCH THE RELEVANT NAMES SELECT @CompanyId = companyId from dbo.keywords with(nolock) WHERE KeywordId = @KeywordId --SELECT SubscriberId,Name into @SubscriberNames from dbo.subscriber_names(@CompanyID) IF @Telco = 0 BEGIN INSERT INTO @SubscribersByTelco SELECT rc.SubscriberID from reports.conversations as rc with(nolock) WHERE rc.KeywordID = @KeywordId AND rc.TelcoID IS NULL --Select @Telco = 'UnKnown' END ELSE IF @Telco -1 BEGIN INSERT INTO @SubscribersByTelco SELECT rc.SubscriberID from reports.conversations as rc with(nolock) WHERE rc.KeywordID = @KeywordId AND rc.TelcoID = @Telco END IF @KeywordListId = 0 BEGIN insert into @ListSubscribers (SubscriberID) SELECT SubscriberID FROM SUBSCRIBERS_NOT_LISTED(@KeywordId) END ELSE IF @KeywordListId > 0 BEGIN insert into @ListSubscribers (SubscriberID) SELECT Subscriberid FROM [dbo].[subscription_lists] as sl with(nolock) INNER JOIN dbo.subscriptions as s with(nolock) ON sl.subscriptionid = s.subscriptionid where keywordlistid in (@KeywordListId) AND s.IsRevoked = 0 END IF @AuthLists '-1' AND @AuthLists '-2' BEGIN IF (select count(*) from Splitter(@AuthLists) where Value = '0') > 0 BEGIN INSERT INTO @AuthorisedListSubscribers select nl.Subscriberid FROM SUBSCRIBERS_NOT_LISTED(@KeywordId) as nl INNER JOIN dbo.subscriptions as su with(nolock) ON nl.Subscriberid = su.Subscriberid AND su.KeywordId = @KeywordId AND su.Status = 'Approved' AND su.IsRevoked = 0 END --SELECT * from @AuthorisedListSubscribers INSERT INTO @AuthorisedListSubscribers SELECT distinct su.SubscriberId FROM dbo.subscriptions as su with(nolock) INNER JOIN dbo.subscription_lists as sl with(nolock) ON su.SubscriptionID = sl.SubscriptionID AND sl.KeywordListID IN ( select * from Splitter(@AuthLists) ) AND su.Status = 'Approved' AND su.IsRevoked = 0 --SELECT * from @AuthorisedListSubscribers END IF (@QuietEnd IS NOT NULL AND @QuietStart IS NULL) BEGIN INSERT INTO @QuietSubscribers SELECT distinct rc.SubscriberID FROM reports.conversations rc with(nolock) INNER JOIN subscriptions ss with(nolock) ON rc.SubscriptionID = ss.SubscriptionID and ss.KeywordID=@KeywordId and ss.CreatedOn=@QuietEnd and rc.KeywordID=@KeywordId) --SELECT p.SubscriberID FROM --( -- SELECT distinct rc.SubscriberID FROM reports.conversations rc -- INNER JOIN subscriptions ss ON rc.SubscriptionID = ss.SubscriptionID and ss.KeywordID=@KeywordId and ss.CreatedOn 0 BEGIN INSERT INTO #AuthorisedListSubscribers select nl.Subscriberid FROM SUBSCRIBERS_NOT_LISTED(@KeywordId) as nl INNER JOIN dbo.subscriptions as su ON nl.Subscriberid = su.Subscriberid AND su.KeywordId = @KeywordId AND su.Status = 'Approved' AND su.IsRevoked = 0 END --SELECT * from @AuthorisedListSubscribers INSERT INTO #AuthorisedListSubscribers SELECT distinct su.SubscriberId FROM dbo.subscriptions as su INNER JOIN dbo.subscription_lists as sl ON su.SubscriptionID = sl.SubscriptionID AND sl.KeywordListID IN ( select * from Splitter(@AuthLists) ) AND su.Status = 'Approved' AND su.IsRevoked = 0 --SELECT * from @AuthorisedListSubscribers END IF (@QuietEnd IS NOT NULL AND @QuietStart IS NULL) BEGIN INSERT INTO #QuietSubscribers SELECT distinct rc.SubscriberID FROM reports.conversations rc INNER JOIN subscriptions ss ON rc.SubscriptionID = ss.SubscriptionID and ss.KeywordID=@KeywordId and ss.CreatedOn=@QuietEnd and rc.KeywordID=@KeywordId) --SELECT p.SubscriberID FROM --( -- SELECT distinct rc.SubscriberID FROM reports.conversations rc -- INNER JOIN subscriptions ss ON rc.SubscriptionID = ss.SubscriptionID and ss.KeywordID=@KeywordId and ss.CreatedOn