Membership Revamp
QI Stats Computation SQL Job (Update New QI Ranks-New Version 5.0.sql )
Tables Involved
Following are the tables which have been involvement into this job. 1. 3. 5. 7. 9. 11. 13. tQISummary tQISummary_hold tQIBidsData tQIBidsData_hold tQICustomerRelationshipData tQICustomerRelationshipData_hold tQICategorySummary 2. 4. 6. 8. 10. 12. 14. tGuruBill tGuruProfile tGuruTransaction tAuthority tGuruApply tGuru tCategory
Work Flow
Step 1 - Populate tQICustomerRelationShipData_hold table data.
TRUNCATE TABLE tQICustomerRelationshipData_hold Insert tQICustomerRelationshipData_hold (profileid,guruid,MainCategoryid,catrank,active,hidden,membertype,Companyid,First PayDate,revenue12, CustAgeActual,custage,revenue12PerMonth) Select z.pro_id,z.[GuruID],z.[mainCategoryID],z.[catrank],z.[active],z.hidden,'MemberTyp e'= CASE z.[membertype] WHEN 0 THEN 0 ELSE 1 END, x.companyid, y.FirstPayDate, x.revenue12, y.CustAgeActual, y.CustomerAge, CAST((x.revenue12/y.customerage) AS DECIMAL(9,2)) AS Revenue12PerMth From ( select b.[GuruID],b.[mainCategoryID],a.companyid,sum(a.received) as revenue12 from tgurubill a join tguruprofile b on a.pro_id=b.pro_id where a.status=1 and a.deleted=0 and a.paydate > @today group by b.[GuruID],b.[mainCategoryID],a.[CompanyID] ) x JOIN (select b.[GuruID],b.[mainCategoryID],a.companyid,min(a.paydate) as FirstPayDate, ceiling(CAST(datediff(day,Min(a.paydate), @today) AS DECIMAL(9,2))/30) as CustAgeActual, 'CustomerAge'= case when ceiling(cast(datediff(day, Min(a.paydate), @today) as decimal(9,2))/30)=0 then 1
when ceiling(cast(datediff(day, Min(a.paydate), @today) as decimal(9,2))/30)>12 then 12 else ceiling(cast(datediff(day, Min(a.paydate), @today) as decimal(9,2))/30) END from tgurubill a join tguruprofile b on a.pro_id=b.pro_id where a.status=1 and a.deleted=0 group by b.[GuruID],b.[mainCategoryID],a.companyid ) y on x.GuruID=y.GuruID AND x.mainCategoryID=y.mainCategoryID and x.companyid=y.companyid JOIN [tGuruProfile] z ON y.[GuruID] = z.[GuruID] AND y.[mainCategoryID] = z.[mainCategoryID] order by z.[mainCategoryID],z.[catrank] GO UPDATE a SET a.adjustedcustage=(CASE WHEN b.MaxcustageActual <=6 then 6 ELSE (CASE WHEN a.CustAgeActual < 12 THEN 12 ELSE a.custAgeActual END) END) FROM [tQICustomerRelationshipData_hold] a JOIN (SELECT profileID, MAX(custAgeActual) AS MaxcustAgeActual FROM [tQICustomerRelationshipData_hold] GROUP BY [profileID]) b ON a.[ProfileID]=b.profileID GO
Note: From the above step, we have to remove the reference of tGuruProfile table and for group by, we will use category of service associated with invoice instead of profiles category. We will stop inserting pro_id into tQICustomerRelationshipData_hold table.
DECLARE @DATE DATETIME DECLARE @TODAY DATETIME SET @TODAY=GETDATE() SET @DATE=DATEADD(YEAR,-1,@TODAY) TRUNCATE TABLE tQICustomerRelationshipData_hold /* Insert all Pro-Employer relationships in the table tQICustomerRelationshipData*/ Insert tQICustomerRelationshipData_hold (guruid,MainCategoryid,catrank,membertype,Companyid,FirstPayDate,revenue12, CustAgeActual,custage,revenue12PerMonth) Select z.[GuruID],z.[CategoryID],z.[CatRank],'MemberType'=CASE w.[MembershipTypeId] WHEN 0 THEN 0 ELSE 1 END, x.companyid, y.FirstPayDate, x.revenue12, y.CustAgeActual, y.CustomerAge, CAST((x.revenue12/y.customerage) AS DECIMAL(9,2)) AS Revenue12PerMth From ( select a.[GuruID],b.[CategoryID],a.[CompanyID],SUM(a.received) as Revenue12 from tGuruBill a join tGuruService b on a.ServiceID=b.ServiceID where a.status=1 and a.deleted=0 and a.paydate > @date group by a.[GuruID],b.[CategoryID],a.[CompanyID] ) x JOIN ( select a.[GuruID],b.[CategoryID],a.CompanyID,MIN(a.paydate) as FirstPayDate,ceiling(CAST(datediff(day,Min(a.paydate),@today) AS DECIMAL(9,2))/30) as CustAgeActual, 'CustomerAge'= case
when ceiling(cast(datediff(day,Min(a.paydate),@today) as decimal(9,2))/30)=0 then 1 when ceiling(cast(datediff(day,Min(a.paydate),@today) as decimal(9,2))/30)>12 then 12 else ceiling(cast(datediff(day,Min(a.paydate),@today) as decimal(9,2))/30) END from tgurubill a join tGuruService b on a.ServiceID=b.ServiceID where a.status=1 and a.deleted=0 group by a.[GuruID],b.[CategoryID],a.[CompanyID] ) y on x.[GuruID]=y.[GuruID] AND x.[CategoryID]=y.[CategoryID] and x.[CompanyID]=y.[CompanyID] JOIN [tGuruService] z ON y.[GuruID] = z.[GuruID] AND y.[CategoryID] = z.[CategoryID] JOIN [tGuruDetail] w ON y.[GuruID] = w.[GuruID] order by z.[ServiceID] GO UPDATE a SET a.AdjustedCustAge=(CASE WHEN b.MaxcustageActual <=6 then 6 ELSE (CASE WHEN a.CustAgeActual < 12 THEN 12 ELSE a.custAgeActual END) END) FROM [tQICustomerRelationshipData_hold] a JOIN (SELECT [GuruID], MAX(custAgeActual) AS MaxcustAgeActual FROM [tQICustomerRelationshipData_hold] GROUP BY [GuruID]) b ON a.[GuruID]=b.[GuruID] GO
Step 2 - Populate tQISummary_hold table data with the Active Pro data
TRUNCATE TABLE tQISummary_hold GO Insert tQISummary_hold(profileid ,guruid,maincategoryid,revenue12 ,customerage ,Revenue12PerMonth ,CompanyCount,Revenue12PerMonthPerCust, WeightedLongevityMonth) Select x.profileid,x.guruid,x.maincategoryid, sum(x.revenue12) as Revenue12,sum(x.custage) as CustomerAge, SUM(x.revenue12PerMonth) as RcdPerMonth,COUNT(x.companyid) as CompanyCnt,SUM(x.revenue12PerMonth)/(CASE when COUNT(x.companyid) < 3 then 3 else COUNT(x.companyid) end )as RcdPerMthPerCust, sum(x.WeightedLongevityMth) as WeightedLongevityMth from (Select b.*,a.totalrevenue12PerMth,(b.revenue12PerMonth/a.totalrevenue12PerMth)* (CASE WHEN a.MaxcustageActual <=6 then 6 ELSE (CASE WHEN b.CustAgeActual < 12 THEN 12 ELSE b.custAgeActual END) END)as WeightedLongevityMth from (Select profileid,sum(revenue12PerMonth) as totalrevenue12PerMth,Max(custAgeActual) as MaxCustAgeActual from tQICustomerRelationShipData_hold group by profileid) a Join tQICustomerRelationShipData_hold b on a.profileid=b.profileid) x group by x.profileid,x.guruid,x.maincategoryid order by x.profileid GO
Note: From the above step, we have to remove the reference of profileId and for group by, we will use category of service associated with invoice instead of profiles category. We will stop inserting ProfileId into tQISummary_hold table.
TRUNCATE TABLE tQISummary_hold GO Insert tQISummary_hold(guruid,maincategoryid,revenue12,customerage ,Revenue12PerMonth,CompanyCount,Revenue12PerMonthPerCust,WeightedLongevityMonth) Select x.guruid,x.maincategoryid,sum(x.revenue12) as Revenue12,sum(x.custage) as CustomerAge,SUM(x.revenue12PerMonth) as RcdPerMonth, COUNT(x.companyid) as CompanyCnt,SUM(x.revenue12PerMonth)/(CASE when COUNT(x.companyid) < 3 then 3 else COUNT(x.companyid) end )as RcdPerMthPerCust, sum(x.WeightedLongevityMth) as WeightedLongevityMth from (Select b.*,a.totalrevenue12PerMth,(b.revenue12PerMonth/a.totalrevenue12PerMth)* (CASE WHEN a.MaxcustageActual <=6 then 6 ELSE (CASE WHEN b.CustAgeActual < 12 THEN 12 ELSE b.custAgeActual END) END)as WeightedLongevityMth from (Select guruid,sum(revenue12PerMonth) as totalrevenue12PerMth,Max(custAgeActual) as MaxCustAgeActual from tQICustomerRelationShipData_hold group by guruid) a Join tQICustomerRelationShipData_hold b on a.guruid=b.guruid) x group by x.guruid,x.maincategoryid order by x.guruid GO
Step 3 - Update tQISummary_hold table data for Profiles with no earnings.
UPDATE a SET a.catrank=b.catrank,a.hidden=b.hidden,a.active=b.active,a.membertype=b.[membertype ],a.revenueall=b.[revnueall] FROM tQISummary_hold a JOIN tguruprofile b ON a.profileid=b.pro_id Update a set a.LastInvoiceDate=b.LastInvoiceDate, a.[HaslastsixmonthsEarnings]=case when datediff(day, b.LastInvoiceDate,getdate()) <= 183 then 1 else 0 end from tQIsummary_hold a join (select c.profileID, Max(b.paydate) as LastInvoiceDate from tQISummary_hold c join tguruprofile a on c.guruID=a.guruID and c.MainCategoryID = a.MainCategoryID join tgurubill b on a.pro_id=b.pro_id where b.Status=1 and b.deleted=0 group by c.profileID) b on a.profileID=b.ProfileID /* Insert ProfileIDs not having revenue in last 12 months */ SET ROWCOUNT 25000 DECLARE @count INT DECLARE @counter INT SELECT @counter=0 SELECT @count= COUNT(*)/25000 FROM tguruprofile with(nolock) WHERE pro_id NOT IN (SELECT profileid FROM tQISummary_hold) WHILE (@counter <= @count) BEGIN INSERT tQISummary_hold(profileid, guruid, maincategoryid,membertype, catrank,hidden, active,revenue12,revenueall, customerage, Revenue12PerMonth, Companycount, Revenue12PerMonthPerCust, WeightedLongevityMonth, [HaslastsixmonthsEarnings] ) SELECT [Pro_ID], [GuruID], [mainCategoryID], [membertype],[catrank],[hidden],
[active], 0, ISNULL(revnueall,0), 0, 0, 0, 0, 0, 0 FROM tguruprofile with(nolock) WHERE pro_id NOT IN (SELECT profileid FROM tQISummary_hold) SET @counter = @counter + 1 END SET ROWCOUNT 0 GO --------------------------------------------SET ROWCOUNT 25000 DECLARE @count INT DECLARE @counter INT SELECT @counter=0 SELECT @count= COUNT(*)/25000 FROM tQISummary_hold WHERE [LastLoginTime] IS NULL WHILE (@counter <= @count) BEGIN UPDATE a SET a.TransactionDate = b.TransactionDate, a.LastLoginTime = c.LastLogintime FROM tQISummary_hold a LEFT JOIN tGuruTransaction b ON a.profileid = b.pro_id JOIN [tAuthority] c ON a.guruid = c.smlid AND c.TYPE='I'and a.LastLogintime is NULL SET @counter=@counter+1 END SET ROWCOUNT 0 GO
Note: From the above step, we have to remove the reference of tGuruProfile table and for group by, we will use category of service associated with invoice instead of profiles category. Also we will use list of freelancer not having revenue in last 12 months in place of profile.
Step 4 - Populate tQIBidsData_hold table data.
TRUNCATE TABLE tQIBidsData_hold INSERT [tQIBidsData_hold] ( [ProjectID], [ProjectName], [InvoicePaiddate], [BidSubmitDate], [CompanyID], [BillID], [ProfileID] ) SELECT final.projectid, final.ProjectName ,final.FirstPaydate,final.dateposted,final.companyid,final.BillID,z.[Pro_ID] FROM (SELECT x.guruid,x.maincategoryID, x.ProjectID,X.BillID,X.DatePosted,Y.FirstPaydate,X.companyID,X.ProjectName ,RANK() OVER (PARTITION BY X.guruid,X.maincategoryID ORDER BY X.datePosted DESC) AS 'RANK' FROM (SELECT b.guruid,b.maincategoryid,a.projectid,c.companyid,a.dateposted,NULL AS BillID,c.Title as ProjectName FROM tguruapply a JOIN tguruprofile b ON a.pro_id=b.[Pro_ID] JOIN tprojects c ON a.projectid=c.projectid WHERE a.projectid IN (SELECT projectid FROM tgurubill WHERE status=1 AND deleted=0 )
UNION SELECT b.guruid,b.maincategoryid,a.projectid,a.companyid,a.paydate,a.billid,a.[Title] as ProjectName FROM [tGuruBill] a JOIN tguruprofile b ON a.pro_id=b.pro_id WHERE a.projectid=0 AND a.status=1 AND a.deleted=0 ) X LEFT OUTER JOIN ( SELECT b.guruid,b.maincategoryid,a.companyid,MIN(a.paydate) AS FirstPaydate FROM tgurubill a JOIN tguruprofile b ON a.[Pro_id]=b.[Pro_ID] WHERE a.status=1 AND a.deleted=0 GROUP BY b.guruid,b.maincategoryid,companyid ) Y ON x.companyid=y.companyid AND x.guruid=y.guruid AND x.maincategoryId=y.maincategoryID WHERE x.Dateposted <= ISNULL(Y.FirstPayDate,getdate()) ) final JOIN tguruprofile Z ON final.guruid=z.guruid AND final.maincategoryID=Z.mainCategoryID WHERE final.[rank] <= 100
Note: From the above step, we have to remove the reference of tGuruProfile table and for group by, we will use category of service associated with invoice instead of profiles category.
Step 5 - Exclude the Projects with no winning bids by updating InvoicePaidDate into tQIBidsdata_hold table.
UPDATE a SET a.InvoicePaidDate= NULL FROM tQIBidsdata_hold a JOIN ( SELECT q.*, p.MinPayDate FROM (SELECT Y.ProfileID,MIN(X.PayDate) AS MinPayDate, X.ProjectID FROM (SELECT a.*,b.ProfileID,b.MainCategoryID as MainCat,b.QiSummaryID FROM tgurubill a JOIN tQisummary b ON a.pro_id=b.profileID WHERE a.status=1 AND a.deleted=0 AND a.[ProjectID] <> 0 ) X JOIN tQiSummary y ON x.GuruID=y.GuruID AND X.mainCat=y.MainCategoryID GROUP BY Y.ProfileID,X.ProjectID) p RIGHT JOIN tqibidsdata_hold q ON p.ProfileID=q.ProfileID AND p.projectID=q.ProjectID WHERE q.InvoicePaidDate IS NOT NULL AND q.projectid <>0 AND p.MinPayDate IS null ) abc ON a.ProfileID = abc.ProfileID AND a.ProjectID = abc.ProjectID
Note: From the above step, we have to remove the reference of ProfileID and for group by, we will use GuruID and ProjectID.
Step 6 - Update BidsConsidered and NewCompanyAdded values into tQISummary_Hold table.
UPDATE a SET a.BidsConsidered = b.ValidBids,NewCompanyAdded = b.NewCompanyCount FROM tQISummary_Hold a JOIN (SELECT ProfileID,COUNT(*) AS validBids, COUNT(InvoicepaidDate) AS NewCompanyCount FROM tQIBidsData_hold GROUP BY ProfileID) b ON a.ProfileID=b.ProfileID
WAITFOR DELAY '00:00:05' UPDATE tqisummary_hold SET BidsConsidered =0, NewCompanyAdded=0,CustAcquisitionRate=0 WHERE BidsConsidered IS NULL
Note: From the above step, we have to remove the reference of ProfileID and for group by, we will use GuruID.
Step 7 - Update AdjustedCAR and CustAcquisitionRate values into tQISummary_Hold table.
UPDATE tQISummary_hold SET [AdjustedCAR] = CAST((CAST(NewCompanyAdded AS DECIMAL(9,4))/CASE WHEN BidsConsidered < 40 THEN 40 ELSE bidsconsidered end) AS DECIMAL(9,4)), CustAcquisitionRate = CAST((CAST(NewCompanyAdded AS DECIMAL(9,4))/BidsConsidered) AS DECIMAL(9,4)) WHERE bidsconsidered > 0 UPDATE tQISummary_hold SET CustAcquisitionrate=0 WHERE bidsconsidered <= 0
Note: No modification required.
Step 8 - Update QI Values into tQISummary_Hold table.
UPDATE tQISummary_hold SET QualityScore=(Revenue12PerMonthPerCust*WeightedLongevityMonth*AdjustedCAR) UPDATE tQISummary_hold SET QualityScore=0 where membertype >0 and qualityscore is null
Note: No modification required.
Step 9 - Rank Order freelancer as per their Quality Score.
SET QUOTED_IDENTIFIER ON GO -- update the GlobalRank used for search "Match" values and category rankings -- and while were touching all the records, reset the category -rank to null UPDATE [dbo].[tQISummary_hold] SET [NewCatRank] = null, [GlobalRank] = r.[rank] FROM [dbo].[tQISUmmary_hold] gp INNER JOIN ( SELECT [Pro_ID], dense_rank() OVER( ORDER BY -- without a linked tauthority record, you go to the bottom CASE WHEN [dbo].[tAuthority].[smlid] IS null THEN 1 ELSE 0 END ASC, -- without a linked tguru record (!?!?), you go to the bottom CASE WHEN [dbo].[tGuru].[GuruID] IS null THEN 1 ELSE 0 END ASC, -- if you''re marked deleted, you get pushed to the bottom CASE WHEN [dbo].[tGuruProfile].deleted = 1 or [dbo].[tGuru].[deleted] =
1 THEN 1 ELSE 0 END ASC, -- basic members are after guru/guru vendor CASE WHEN [dbo].[tGuruProfile].[MemberType] = 0 THEN 1 ELSE 0 END ASC, -- quality score is the primary differentiator between profiles - but if you don''t have earnings in the last six months, your -- quality score is treated as 0. isnull(CASE WHEN [dbo].[tQiSummary_hold].[HasLastSixMonthsEarnings] = 1 THEN [dbo].[tQiSummary_hold].[qualityscore] ELSE 0 END, 0) DESC, -- and if those aren''t different, we use the time since last login (recent is better) isnull([dbo].[tAuthority].[lastlogintime], cast(''1900-01-01'' AS datetime)) DESC, -- we move to the guru account''s signup date (longest guru get''s benefit) isnull([dbo].[tGuru].[SignupDate], getDate()) ASC, -- and finally, we use the guru''s id itself just as a paranoia -- that two freelancers don''t end up tied with each other (note: -- two profiles for the same guru might still end up tied; the -- dense_rank() function will assign them the same rank). [dbo].[tGuruProfile].[GuruID] ASC ) AS [rank] FROM [dbo].[tGuruProfile] LEFT JOIN [dbo].[tGuru] ON [dbo].[tGuruProfile].[GuruID] = [dbo].[tGuru].[GuruID] LEFT JOIN [dbo].[tQiSummary_hold] ON [dbo].[tGuruProfile].[pro_id] = [dbo].[tQiSummary_hold].[profileID] LEFT JOIN [dbo].[tAuthority] ON [dbo].[tGuru].[GuruID]=[dbo].[tAuthority].[smlid] AND [dbo].[tAuthority].[Type]=''I'' ) AS r ON r.[Pro_ID] = gp.[ProfileID] GO UPDATE [dbo].[tQISummary_hold] SET [NewcatRank]= r.RANK FROM tQISummary_hold gp INNER JOIN ( SELECT profileid, rank() OVER(PARTITION BY [mainCategoryID] ORDER BY [GlobalRank] ) AS [RANK] FROM [tQISummary_hold] WHERE [profileid] IN (SELECT [pro_id] FROM [dbo].[vSearchableProfiles])) AS r ON gp.profileID=r.profileid GO
Note: From the above step, we have to remove the reference of tGuruProfile table and modification required to make GlobalRank calculation at account level.
10 - Find Rank Order Percentile.
DECLARE @CategoryID INT DECLARE c_cat CURSOR FOR SELECT CategoryID FROM [tCategory] WHERE mainid=0 OPEN c_cat FETCH NEXT FROM c_cat INTO @CategoryID PRINT @CategoryID WHILE @@FETCH_STATUS =0 BEGIN PRINT @CategoryID IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE NAME LIKE '%#temp_rank%') DROP TABLE #temp_rank
CREATE TABLE #temp_rank(id INT IDENTITY(1,1) NOT NULL,ProfileID INT,Percentile DECIMAL(9,2)) INSERT INTO #temp_Rank([ProfileID]) SELECT [ProfileID] FROM tqisummary_hold a WHERE a.maincategoryid =@categoryid AND QualityScore >0 ORDER BY a.Qualityscore DESC DECLARE @ProfileCount int SELECT @ProfileCount=COUNT(*) FROM [#temp_rank] UPDATE #temp_rank SET [Percentile]=FLOOR((cast((@ProfileCount-ID) AS DECIMAL(9,2))/@ProfileCount)*100) UPDATE tQISummary_hold SET QICategoryPercentile=[#temp_rank].[Percentile] FROM tQISummary_hold INNER JOIN #temp_rank ON (tQiSummary_hold.ProfileID=[#temp_rank].[ProfileID]) UPDATE tQISummary_hold SET QICategoryPercentile= (SELECT TOP 1 QICategoryPercentile FROM tQiSummary_hold a WHERE a.guruid=tQiSummary_hold.guruid AND a.maincategoryid=@categoryid AND a.QICategoryPercentile > 0 ORDER BY QICategoryPercentile Desc) WHERE QICategoryPercentile >0 AND maincategoryid=@CategoryID DROP TABLE #temp_rank FETCH NEXT FROM c_cat INTO @CategoryID END CLOSE c_cat DEALLOCATE c_cat GO
Note: From the above step, we have to remove the reference of ProfileID and will use GuruID in place of that.
Step 11 - Rank Order CER
DECLARE @CategoryID INT DECLARE c_cat CURSOR FOR SELECT CategoryID FROM [tCategory] WHERE mainID = 0 OPEN c_cat FETCH NEXT FROM c_cat INTO @CategoryID PRINT @CategoryID WHILE @@FETCH_STATUS =0 BEGIN PRINT @CategoryID IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE NAME LIKE '%#temp_rank%') DROP TABLE #temp_rank CREATE TABLE #temp_rank(id INT IDENTITY(1,1) NOT NULL,ProfileID INT,Percentile DECIMAL(9,2)) INSERT INTO #temp_Rank([ProfileID]) SELECT a.[ProfileID] FROM tqisummary_hold a WHERE
a.maincategoryid =@categoryid AND a.Revenue12PerMonthPerCust >0 ORDER BY a.Revenue12PerMonthPerCust DESC DECLARE @ProfileCount int SELECT @ProfileCount=COUNT(*) FROM [#temp_rank] UPDATE #temp_rank SET [Percentile]=FLOOR((cast((@ProfileCount-ID) AS DECIMAL(9,2))/@ProfileCount)*100) UPDATE tQISummary_hold SET CERCategoryPercentile=[#temp_rank].[Percentile] FROM tQISummary_hold INNER JOIN #temp_rank ON (tQiSummary_hold.ProfileID=[#temp_rank].[ProfileID]) UPDATE tQISummary_hold SET CERCategoryPercentile= (SELECT TOP 1 CERCategoryPercentile FROM tQiSummary_hold a WHERE a.guruid=tQISummary_hold.guruid AND a.maincategoryid=@categoryid AND a.CERCategoryPercentile > 0 ORDER BY CERCategoryPercentile DESC,Revenue12PerMonthPerCust DESC) WHERE CERCategoryPercentile >0 AND maincategoryid=@CategoryID DROP TABLE #temp_rank FETCH NEXT FROM c_cat INTO @CategoryID END CLOSE c_cat DEALLOCATE c_cat GO
Note: From the above step, we have to remove the reference of ProfileID and will use GuruID in place of that.
Step 12 - Rank Order CRR (Weighted Longevity in Months)
DECLARE @CategoryID INT DECLARE c_cat CURSOR FOR SELECT CategoryID FROM [tCategory] WHERE [mainID]= 0 OPEN c_cat FETCH NEXT FROM c_cat INTO @CategoryID PRINT @CategoryID WHILE @@FETCH_STATUS =0 BEGIN PRINT @CategoryID IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE NAME LIKE '%#temp_rank%') DROP TABLE #temp_rank CREATE TABLE #temp_rank(id INT IDENTITY(1,1) NOT NULL,ProfileID INT,Percentile DECIMAL(9,2)) INSERT INTO #temp_Rank([ProfileID]) SELECT [ProfileID] FROM tqisummary_hold a WHERE a.maincategoryid =@categoryid AND a.WeightedLongevityMonth >0 ORDER BY a.WeightedLongevityMonth DESC,a.revenueall desc
DECLARE @ProfileCount int SELECT @ProfileCount=COUNT(*) FROM [#temp_rank] UPDATE #temp_rank SET [Percentile]=FLOOR((cast((@ProfileCount-ID) AS DECIMAL(9,2))/@ProfileCount)*100) UPDATE tQISummary_hold SET CRRCategoryPercentile=[#temp_rank].[Percentile] FROM tQISummary_hold INNER JOIN #temp_rank ON (tQiSummary_hold.ProfileID=[#temp_rank].[ProfileID]) UPDATE tQISummary_hold SET CRRCategoryPercentile= (SELECT TOP 1 CRRCategoryPercentile FROM tQiSummary_hold a WHERE a.guruid=tQISummary_hold.guruid AND a.maincategoryid=@categoryid AND a.CERCategoryPercentile > 0 ORDER BY CRRCategoryPercentile DESC) WHERE CERCategoryPercentile >0 AND maincategoryid=@CategoryID DROP TABLE #temp_rank FETCH NEXT FROM c_cat INTO @CategoryID END CLOSE c_cat DEALLOCATE c_cat GO
Note: From the above step, we have to remove the reference of ProfileID and use GuruID in place of that.
Step 13 - Rank Order and Calculate Percentile for CAR
DECLARE @CategoryID INT DECLARE c_cat CURSOR FOR SELECT CategoryID FROM [tCategory] WHERE [mainID] = 0 OPEN c_cat FETCH NEXT FROM c_cat INTO @CategoryID PRINT @CategoryID WHILE @@FETCH_STATUS =0 BEGIN PRINT @CategoryID IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE NAME LIKE '%#temp_rank%') DROP TABLE #temp_rank CREATE TABLE #temp_rank(id INT IDENTITY(1,1) NOT NULL,ProfileID INT,Percentile DECIMAL(9,2)) INSERT INTO #temp_Rank([ProfileID]) SELECT [ProfileID] FROM tqisummary_hold a WHERE a.maincategoryid =@categoryid AND adjustedCAR>0 ORDER BY a.AdjustedCAR DESC,revenue12 desc,revenueall DESC DECLARE @ProfileCount int SELECT @ProfileCount=COUNT(*) FROM [#temp_rank]
UPDATE #temp_rank SET [Percentile]=FLOOR((cast((@ProfileCount-ID) AS DECIMAL(9,2))/@ProfileCount)*100) UPDATE tQISummary_hold SET CARCategoryPercentile=[#temp_rank].[Percentile] FROM tQISummary_hold INNER JOIN #temp_rank ON (tQiSummary_hold.ProfileID=[#temp_rank].[ProfileID]) UPDATE tQISummary_hold SET CARCategoryPercentile= (SELECT TOP 1 CARCategoryPercentile FROM tQiSummary_hold a WHERE a.guruid=tQISummary_hold.guruid AND a.maincategoryid=@categoryid AND a.CARCategoryPercentile > 0 ORDER BY CARCategoryPercentile DESC) WHERE CARCategoryPercentile >0 AND maincategoryid=@CategoryID DROP TABLE #temp_rank FETCH NEXT FROM c_cat INTO @CategoryID END CLOSE c_cat DEALLOCATE c_cat GO
Note: From the above step, we have to remove the reference of ProfileID and use GuruID in place of that.
Step 14 - Populate the tQiSummary from tQISummary_hold Tables for incremental Pros
SET QUOTED_IDENTIFIER ON INSERT tQISummary(ProfileID,GuruID,MainCategoryId,membertype,catrank,hidden,active,revenu e12,revenueall,CustomerAge,Revenue12PerMonth,CompanyCount, Revenue12PerMonthPerCust,WeightedLongevityMonth,TotalBids,BidsConsidered,NewCompan yadded,CustAcquisitionRate,AdjustedCAR,IsAdjusted, QualityScore,NewCatRank,Tier,CARCategoryPercentile,CERCategoryPercentile,CRRCatego ryPercentile,QICategoryPercentile,TransactionDate,LastLoginTime, LastInvoiceDate,[HaslastsixmonthsEarnings],Globalrank) SELECT * FROM tQiSummary_hold WHERE ProfileID NOT IN (SELECT profileid FROM tQISummary)
Note: From the above step, we have to remove the reference of ProfileID and use GuruID in place of that.
Step 15 - Update tQISummary with the New QI and Percentile values.
CREATE TABLE #temp(id INT IDENTITY(1,1),Profileid INT ) insert #temp( ProfileID ) SELECT a.profileid FROM [tQISummary] a JOIN [tQISummary_hold] b ON
a.profileid=b.profileid WHERE a.membertype <> b.membertype OR a.hidden <> b.hidden OR a.active <> b.active OR a.revenue12<> b.revenue12 OR a.revenueall <> b.revenueall OR a.customerage <> b.customerage OR a.revenue12PerMonth <> b.revenue12PerMonth OR a.companycount <> b.companycount OR a.revenue12PerMonthPerCust <> b.revenue12PerMonthPerCust OR a.WeightedLongevityMonth <> b.WeightedLongevityMonth OR a.TotalBids <> b.TotalBids OR a.BidsConsidered <> b.BidsConsidered OR a.NewCompanyadded <> b.NewCompanyadded OR a.CustAcquisitionRate <> b.CustAcquisitionRate OR a.AdjustedCAR <> b.AdjustedCAR OR a.IsAdjusted <> b. IsAdjusted OR a.QualityScore <> b.QualityScore OR a.Tier <> b.Tier OR a.CARCategoryPercentile <> b.CARCategoryPercentile OR a.CERCategoryPercentile <> b.CERCategoryPercentile OR a.CRRCategoryPercentile <> b.CRRCategoryPercentile OR a.QICategoryPercentile <> b.QICategoryPercentile OR a.TransactionDate <> b. TransactionDate OR a.LastLoginTime <> b. LastLoginTime OR a.LastInvoiceDate <> b.LastInvoiceDate OR a.HaslastsixmonthsEarnings <> b.HaslastsixmonthsEarnings OR a.ProposalTypeID <> CASE WHEN b.[QICategoryPercentile] BETWEEN 0 AND 20 THEN 6 WHEN b.[QICategoryPercentile] BETWEEN 21 AND 40 THEN 5 WHEN b.[QICategoryPercentile] BETWEEN 41 AND 60 THEN 4 WHEN b.[QICategoryPercentile] BETWEEN 61 AND 80 THEN 3 WHEN b.[QICategoryPercentile] BETWEEN 81 AND 100 THEN 2 ELSE 6 END GO SELECT COUNT(*) FROM #temp GO
set quoted_identifier on GO declare @count INT declare @batchsize INT declare @loopCount INT declare @counter INT Select @batchsize=100,@counter=0 select @loopCount=max(ID)/@batchsize from [#temp] While (@counter <= @loopcount) begin print @counter UPDATE a SET
a.MainCategoryId=b.MainCategoryId, a.membertype=b.membertype, a.hidden=b.hidden, a.active=b.active, a.revenue12=b.revenue12, a.revenueall=b.revenueall, a.CustomerAge=b.CustomerAge, a.Revenue12PerMonth=b.Revenue12PerMonth, a.CompanyCount=b.CompanyCount, a.Revenue12PerMonthPerCust=b.Revenue12PerMonthPerCust, a.WeightedLongevityMonth=b.WeightedLongevityMonth, a.TotalBids=b.TotalBids, a.BidsConsidered=b.BidsConsidered, a.NewCompanyadded=b.NewCompanyadded, a.CustAcquisitionRate=b.CustAcquisitionRate, a.AdjustedCAR=b.AdjustedCAR, a.IsAdjusted=b.IsAdjusted, a.QualityScore=b.QualityScore, a.Tier=b.Tier, a.CARCategoryPercentile=b.CARCategoryPercentile, a.CERCategoryPercentile=b.CERCategoryPercentile, a.CRRCategoryPercentile=b.CRRCategoryPercentile, a.QICategoryPercentile=b.QICategoryPercentile, a.ProposaltypeID= CASE WHEN b.[QICategoryPercentile] BETWEEN 0 AND 20 THEN 6 WHEN b.[QICategoryPercentile] BETWEEN 21 AND 40 THEN 5 WHEN b.[QICategoryPercentile] BETWEEN 41 AND 60 THEN 4 WHEN b.[QICategoryPercentile] BETWEEN 61 AND 80 THEN 3 WHEN b.[QICategoryPercentile] BETWEEN 81 AND 100 THEN 2 ELSE 6 END, a.TransactionDate=b.TransactionDate, a.LastLoginTime=b.LastLoginTime, a.LastInvoiceDate=b.LastInvoiceDate, a.[HaslastsixmonthsEarnings]=b.[HaslastsixmonthsEarnings] FROM tQISummary a JOIN tQISummary_hold b ON a.profileid=b.profileid JOIN [#temp] c ON a.[ProfileID]=c.[Profileid] where c.[id] between @counter*@batchsize + 1 and (@counter+1)*@batchsize set @counter=@counter+1 END
set quoted_identifier on GO declare @count INT declare @batchsize INT declare @loopCount INT declare @counter INT Select @batchsize=10000,@counter=0 select @loopCount=max(qisummaryid)/@batchsize from tqisummary While (@counter <= @loopcount) begin print @counter UPDATE a SET a.catrank=b.catrank, a.[NewCatRank]=b.newcatrank, a.Globalrank=b.Globalrank FROM tQISummary a JOIN tQISummary_hold b ON a.profileid=b.profileid
WHERE a.qisummaryid between @counter*@batchsize + 1 and (@counter+1)*@batchsize print (@counter+1)*@batchsize set @counter=@counter+1 END GO
DROP TABLE GO
#temp
Note: From the above step, we have to remove the reference of ProfileID and will use GuruID for joining the tables.
Step 16 - Populate tQICustomerRelationship table data.
TRUNCATE TABLE tQICustomerRelationshipData INSERT tQICustomerRelationshipData(QISummaryID, ProfileID, GuruID, MainCategoryID, Membertype, Catrank, Active, Hidden, CompanyID, CompanyName, Revenue12, FirstPayDate, CustAgeActual, CustAge, Revenue12PerMonth, AdjustedCustAge) SELECT b.QISummaryID,a.* FROM tQICustomerRelationshipData_hold a JOIN tqisummary b ON a.profileid=b.profileid
Note: From the above step, we have to remove the reference of ProfileID and will use GuruID for joining the tables.
Step 17 - Update tQIBidsData table data.
TRUNCATE TABLE tQIBidsData GO SET ROWCOUNT 0 GO DECLARE @count INT DECLARE @counter INT DECLARE @StartCount INT DECLARE @EndCount INT SELECT @StartCount=1,@EndCount=25000,@counter=0 SELECT @count= MAX([ProfileID])/25000 FROM tqisummary WHILE (@counter <= @count) BEGIN INSERT tQiBidsData (QISummaryID, ProjectID, ProjectName, InvoicePaiddate, BidSubmitDate, CompanyID,BillID) SELECT b.QISummaryID,a.[ProjectID],a.[ProjectName],a.[InvoicePaiddate],a.[BidSubmitDate], a.[CompanyID],a.[BillID] FROM tQIBidsData_hold a JOIN tQiSummary b ON a.profileID=b.profileID
WHERE b.profileid BETWEEN @StartCount AND @EndCount SELECT @StartCount=@StartCount+25000,@EndCount=@EndCount+25000,@counter=@counter+1 WAITFOR DELAY END SET ROWCOUNT 0 GO '00:00:01'
Note: From the above step, we have to remove the reference of ProfileID and use GuruID for joining the tables.
Step 18 - Update tQICategorySummary table data.
TRUNCATE TABLE tQICategorySummary INSERT tQICategorySummary(CategoryID,CERMax,CERMin,CRRMax,CRRMin,CARMax,CARMin,QIMax,QIMi n,DateUpdated) SELECT MaincategoryID,MAX(Revenue12PerMonthPerCust),MIN(Revenue12PerMonthPerCust), MAX(WeightedLongevityMonth),MIN(WeightedLongevityMonth), MAX(AdjustedCAR),MIN(AdjustedCAR),MAX(QualityScore),MIN(QualityScore),GETDATE() FROM tqiSummary GROUP BY maincategoryID GO
Note: No modification required. Step 19 - Update tGuruProfile table with new ranks
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO DECLARE @MaxProfileID INT DECLARE @startID INT DECLARE @endID INT SELECT @startId =0 SELECT @endID =0 SELECT @MaxProfileID=MAX(pro_id) FROM [tGuruProfile] WHILE(@endID < @MaxProfileID) BEGIN SELECT @startID=@endID+1,@endID=@endID+1000 UPDATE tguruprofile SET [catRank] =r.NewcatRANK, [Globalrank]= r.Globalrank FROM tGuruProfile gp INNER JOIN tQIsummary_hold AS r ON gp.pro_id=r.profileid WHERE gp.[Pro_ID] BETWEEN @startID AND @EndID END GO
Note: From now onward, we will maintain GlobalRank into tGuruDetail table.
Step 20 - Reorganize indexes
SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON DECLARE DECLARE DECLARE DECLARE DECLARE declare @TableName VARCHAR(100) @IndexName VARCHAR(100) @avg_fragmentation_in_percent REAL @page_count INT @sql NVARCHAR(1000) @err Int
DECLARE c_defrag CURSOR FOR SELECT OBJECT_NAME(a.Object_id),b.NAME,a.avg_fragmentation_in_percent,a.page_count FROM sys.dm_db_index_physical_stats(DB_ID('gurusql'),object_id('vsearchableprofile'),NU LL,NULL,NULL) a JOIN sys.indexes b ON a.[OBJECT_ID]=b.[OBJECT_ID] AND a.index_id=b.index_id WHERE avg_fragmentation_in_percent >10 AND page_count >500 AND index_type_desc <> 'HEAP' OPEN c_defrag FETCH NEXT FROM c_defrag INTO @TableName,@IndexName,@avg_fragmentation_in_percent,@page_count WHILE (@@FETCH_STATUS=0) BEGIN SET @sql='ALTER INDEX ' + @IndexName + ' ON ' + @TableName + ' REORGANIZE' EXEC sp_executesql @sql FETCH NEXT FROM c_defrag INTO @TableName,@IndexName,@avg_fragmentation_in_percent,@page_count END CLOSE c_defrag DEALLOCATE c_defrag
Note: No modification required.
Explanation Step 9: We will first group by the records of tQISummary_Hold table on the basis of GuruID to compute the quality score at account level and then we will determine the rank of freelancer using that result set. Step 10 - 13: Yes, It is to compute the percentile info at the category level. Currently we are picking records from tQISummary_Hold table for each category and computing percentile on the basis of number of profile count in each category and we are using top profiles percentile in the case freelancer having multiple profiles within same category. Now we will compute percentile on the basis of freelancer count and also at account level we dont have multiple entries for a freelancer with same category.