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.
| Author |
Topic |
|
mem
Starting Member
28 Posts |
Posted - 2004-04-21 : 14:50:33
|
| Hello,I have this report that displays data in somewhat of a tiered format.Manager Name | Supervisor Name + additional data | Totals rowOk, basically i have to iterate through each manager to display their supervisors data. Of course all the data is spread across 4 tables. The only way that I know how to do this is via a dreaded cursor. Code is below:create PROCEDURE dbo.MonthlyCenterReport @varReport_Month datetime ASBEGINSET NOCOUNT ONDECLARE @varMgr_ID intDECLARE @varMgrFName_MgrLName varchar(50)DECLARE @MgrID_List varchar(4)DECLARE @MgrFName_List varchar(25)DECLARE @MgrLName_List varchar(25)DECLARE MgrList_Cursor CURSOR FAST_FORWARD FORSELECT Mgr_ID, Mgr_FName, Mgr_LNameFROM tblMonthlyReporting_MgrsWHERE tblMonthlyReporting_Mgrs.Reporting_Month = @varReport_MonthAND Term = 0AND Active = 1ORDER BY Mgr_LNameOPEN MgrList_CursorFETCH NEXT FROM MgrList_CursorINTO @MgrID_List, @MgrFName_List, @MgrLName_ListWHILE (@@FETCH_STATUS = 0) BEGIN SET @varMgr_ID = @MgrID_List SET @varMgrFName_MgrLName = @MgrFName_List + ' ' + @MgrLName_List--manager's names headerSELECT @varMgrFName_MgrLName AS 'Manager'--headers and dataSELECT Q1.Sup_Name AS 'Supervisor', Number_Of_CN AS 'Contract Renewals', Number_Of_NS AS 'New Services', Number_Of_PP AS 'Pre-Paids', Total_Sales AS 'Total Sales', IsNull(CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( CAST(Number_Of_CN AS decimal(8,2)) / CAST(Total_Sales AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS 'Contract Renewal % Pene', Monthly_Goal AS 'Goal', CASE IsNull(Monthly_Goal, 0) WHEN 0 THEN 0 ELSE CAST( CAST(Total_Sales AS decimal(8,2)) / CAST(Monthly_Goal as decimal(8,2)) * 100 AS decimal(8,2)) END AS '% Goal', Number_Of_Data AS 'Data', IsNull(CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( CAST(Number_Of_Data AS decimal(8,2)) / CAST(Total_Sales AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS 'Data % Pene', IsNull(CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( (CAST(Number_Of_Data AS decimal(8,2)) + CAST(Number_Of_NData AS decimal(8,2))) / CAST(Total_Sales AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS 'EF/TS % Pene', IsNull(Acc_Count, 0) AS 'Acc', IsNull(CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( CAST(Acc_Count AS decimal(8,2)) / CAST(Total_Sales AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS 'Acc/TS % Pene', IsNull(CASE Acc_Count WHEN 0 THEN 0 ELSE CAST( CAST(Acc_ComboPack AS decimal(8,2)) / CAST(Acc_Count AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS 'Acc CP/Acc % Pene', IsNull(CASE Handset_Count WHEN 0 THEN 0 ELSE CAST( CAST(Acc_Count AS decimal(8,2)) / CAST(Handset_Count AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS 'Acc/HS % Pene', IsNull(CASE Handset_Count WHEN 0 THEN 0 ELSE CAST( CAST(Acc_Revenue AS decimal(8,2)) / CAST(Handset_Count AS decimal(8,2)) AS decimal(8,2)) END, 0) AS 'Acc Rev/HS', IsNull(CASE Handset_Count WHEN 0 THEN 0 ELSE CAST( CAST(Handset_Count_GT_79 AS decimal(8,2)) / CAST(Handset_Count AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS '$79 HS % Pene', IsNull(CASE Total_CN_NS WHEN 0 THEN 0 ELSE CAST( CAST(AR_Count_GT_45 AS decimal(8,2)) / CAST(Total_CN_NS AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS '$45 % Pene', IsNull(CASE Total_CN_NS WHEN 0 THEN 0 ELSE CAST( CAST(AR_Count_GT_55 AS decimal(8,2)) / CAST(Total_CN_NS AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS '$55 % Pene', IsNull(CASE Number_Of_NS WHEN 0 THEN 0 ELSE CAST( CAST(TwoYR_Count AS decimal(8,2)) / CAST(Number_Of_NS AS decimal(8,2)) * 100 AS decimal(8,2)) END, 0) AS '2 Yr % Pene'FROM ( SELECT TOP 100 PERCENT tblMonthlyReporting_Sups.Sup_FName + ' ' + tblMonthlyReporting_Sups.Sup_LName AS Sup_Name, tblMonthlyReporting_Sups.Sup_LName AS OrderBYLName, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 2 THEN 1 ELSE 0 END) AS Number_Of_CN, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 1 THEN 1 ELSE 0 END) AS Number_Of_NS, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 4 THEN 1 ELSE 0 END) AS Number_Of_PP, SUM(CASE WHEN tblTransactions.Sales_Type_ID <> 4 THEN Data_Feature_Count ELSE 0 END) AS Number_Of_Data, SUM(CASE WHEN tblTransactions.Sales_Type_ID <> 4 THEN NData_Feature_Count ELSE 0 END) AS Number_Of_NData, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 1 THEN 1 WHEN tblTransactions.Sales_Type_ID = 2 THEN 1 WHEN tblTransactions.Sales_Type_ID = 4 THEN 1 ELSE 0 END) AS Total_Sales, IsNull(SUM(Acc_Quantity), 0) AS Acc_Count, SUM(CASE WHEN Combo_Pack = 1 THEN 1 ELSE 0 END) AS Acc_ComboPack, IsNull(SUM(Acc_Revenue), 0) AS Acc_Revenue, SUM(CASE WHEN tblTransactions.Handset_Revenue <> 0 AND Service_Only = 0 THEN 1 ELSE 0 END) AS Handset_Count, SUM(CASE WHEN tblTransactions.Handset_Revenue >= 79.99 AND Service_Only = 0 THEN 1 ELSE 0 END) AS Handset_Count_GT_79, SUM(CASE WHEN tblTransactions.Access_Rate >= 45 THEN 1 ELSE 0 END) AS AR_Count_GT_45, SUM(CASE WHEN tblTransactions.Sales_Type_ID BETWEEN 1 AND 2 THEN 1 ELSE 0 END) AS Total_CN_NS, SUM(CASE WHEN tblTransactions.Access_Rate >= 55 THEN 1 ELSE 0 END) AS AR_Count_GT_55, SUM(CASE WHEN tblTransactions.Contract_Length = 2 AND tblTransactions.Sales_Type_ID = 1 THEN 1 ELSE 0 END) AS TwoYR_Count FROM tblMonthlyReporting_Sups LEFT JOIN tblMonthlyReporting_Reps ON tblMonthlyReporting_Sups.Sup_ID = tblMonthlyReporting_Reps.Sup_ID AND tblMonthlyReporting_Reps.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Reps.Term = 0 AND tblMonthlyReporting_Reps.Active = 1 LEFT JOIN tblTransactions ON tblMonthlyReporting_Reps.Session_ID = tblTransactions.Session_ID AND CONVERT(varchar, DATEADD(day, (-CAST(DAY(tblTransactions.Order_Date) AS int)+1), tblTransactions.Order_Date), 101) = @varReport_Month AND tblTransactions.Active = 1 WHERE tblMonthlyReporting_Sups.Mgr_ID = @varMgr_ID AND tblMonthlyReporting_Sups.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Sups.Term = 0 AND tblMonthlyReporting_Sups.Active = 1 GROUP BY tblMonthlyReporting_Sups.Sup_LName, dbo.tblMonthlyReporting_Sups.Sup_FName ORDER BY tblMonthlyReporting_Sups.Sup_LName ) AS Q1JOIN ( SELECT IQ1.Sup_Name, IsNull(SUM(IQ1.Gross_Acts_Quota), 0) AS Monthly_Goal FROM( SELECT TOP 100 PERCENT tblMonthlyReporting_Sups.Sup_FName + ' ' + tblMonthlyReporting_Sups.Sup_LName AS Sup_Name, tblQuotas.Gross_Acts_Quota FROM tblMonthlyReporting_Sups LEFT JOIN tblMonthlyReporting_Reps ON tblMonthlyReporting_Sups.Sup_ID = tblMonthlyReporting_Reps.Sup_ID AND tblMonthlyReporting_Reps.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Reps.Term = 0 AND tblMonthlyReporting_Reps.Active = 1 LEFT JOIN tblQuotas ON tblMonthlyReporting_Reps.Session_ID = tblQuotas.Session_ID AND tblQuotas.Quota_Effective_Date = @varReport_Month WHERE tblMonthlyReporting_Sups.Mgr_ID = @varMgr_ID AND tblMonthlyReporting_Sups.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Sups.Term = 0 AND tblMonthlyReporting_Sups.Active <> 0 GROUP BY tblMonthlyReporting_Reps.Session_ID, tblMonthlyReporting_Sups.Sup_LName, tblMonthlyReporting_Sups.Sup_FName, tblQuotas.Gross_Acts_Quota ORDER BY tblMonthlyReporting_Sups.Sup_LName ) AS IQ1 GROUP BY IQ1.Sup_Name ) AS Q2 on Q2.Sup_Name = Q1.Sup_NameORDER BY Q1.OrderBYLName------------------------------------------------------------------------------Totals----------------------------------------------------------------------------SELECT IsNull(SUM(Number_Of_CN), 0) AS Total_CN, IsNull(SUM(Number_Of_NS), 0) AS Total_NS, IsNull(SUM(Number_Of_PP), 0) AS Total_PP, IsNull(SUM(Total_Sales), 0) AS Sum_Of_Total_Sales, CASE IsNull(SUM(Total_Sales), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Number_Of_CN) AS decimal(8,2)) / CAST(SUM(Total_Sales) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_CR_Pene, IsNull(SUM(Monthly_Goal), 0) AS Sum_Of_Monthly_Goal, CASE IsNull(SUM(Monthly_Goal), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Total_Sales) AS decimal(8,2)) / CAST(SUM(Monthly_Goal) as decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_Monthly_PGoal, IsNull(SUM(Number_Of_Data), 0) AS Total_Data, CASE IsNull(SUM(Total_Sales), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Number_Of_Data) AS decimal(8,2)) / CAST(SUM(Total_Sales) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_Data_Pene, CASE IsNull(SUM(Total_Sales), 0) WHEN 0 THEN 0 ELSE CAST( (CAST(SUM(Number_Of_Data) AS decimal(8,2)) + CAST(SUM(N_NDATA) AS decimal(8,2))) / CAST(SUM(Total_Sales) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_EF_TS_Pene, IsNull(SUM(Acc_Count), 0) AS Total_AC, CASE IsNull(SUM(Total_Sales), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Acc_Count) AS decimal(8,2)) / CAST(SUM(Total_Sales) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_Acc_TS_Pene, CASE IsNull(SUM(Acc_Count), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Acc_CP) AS decimal(8,2)) / CAST(SUM(Acc_Count) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Acc_ComboPack_Acc_Pene, CASE IsNull(SUM(HC), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Acc_Count) AS decimal(8,2)) / CAST(SUM(HC) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_Acc_Pene, CASE IsNull(SUM(HC), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(Acc_Rev) AS decimal(8,2)) / CAST(SUM(HC) AS decimal(8,2)) AS decimal(8,2)) END AS Total_Acc_Rev_Pene, CASE IsNull(SUM(HC), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(HCGT79) AS decimal(8,2)) / CAST(SUM(HC) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_H_Pene, CASE IsNull(SUM(TCN), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(ACGT45) AS decimal(8,2)) / CAST(SUM(TCN) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_AR45_Pene, CASE IsNull(SUM(TCN), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(ACGT55) AS decimal(8,2)) / CAST(SUM(TCN) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_AR55_Pene, CASE IsNull(SUM(Number_Of_NS), 0) WHEN 0 THEN 0 ELSE CAST( CAST(SUM(TYC) AS decimal(8,2)) / CAST(SUM(Number_Of_NS) AS decimal(8,2)) * 100 AS decimal(8,2)) END AS Total_2YR_PeneFROM ( SELECT QT1.Sup_Name, Number_Of_CN, Number_Of_NS, Number_Of_PP, Total_Sales, CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( CAST(Number_Of_CN AS decimal(8,2)) / CAST(Total_Sales AS decimal(8,2)) AS decimal(8,2)) END AS CR_Pene, Monthly_Goal, CASE Monthly_Goal WHEN 0 THEN 0 ELSE CAST( CAST(Total_Sales AS decimal(8,2)) / CAST(Monthly_Goal as decimal(8,2)) AS decimal(8,2)) END AS Monthly_PGoal, Number_Of_Data, CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( CAST(Number_Of_Data AS decimal(8,2)) / CAST(Total_Sales AS decimal(8,2)) AS decimal(8,2)) END AS Data_Penetration, CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( (CAST(Number_Of_Data AS decimal(8,2)) + CAST(Number_Of_NData AS decimal(8,2))) / CAST(Total_Sales AS decimal(8,2)) AS decimal(8,2)) END AS EF_TS_Pene, Acc_Count, CASE Total_Sales WHEN 0 THEN 0 ELSE CAST( CAST(Acc_Count AS decimal(8,2)) / CAST(Total_Sales AS decimal(8,2)) AS decimal(8,2)) END AS Acc_TS_Pene, CASE Acc_Count WHEN 0 THEN 0 ELSE CAST( CAST(Acc_ComboPack AS decimal(8,2)) / CAST(Acc_Count AS decimal(8,2)) AS decimal(8,2)) END AS AccCP_Acc_Pene, CASE Handset_Count WHEN 0 THEN 0 ELSE CAST( CAST(Acc_Count AS decimal(8,2)) / CAST(Handset_Count AS decimal(8,2)) AS decimal(8,2)) END AS Acc_HS_Pene, CASE Handset_Count WHEN 0 THEN 0 ELSE CAST( CAST(Acc_Revenue AS decimal(8,2)) / CAST(Handset_Count AS decimal(8,2)) AS decimal(8,2)) END AS Acc_Rev_Pene, CASE Handset_Count WHEN 0 THEN 0 ELSE CAST( CAST(Handset_Count_GT_79 AS decimal(8,2)) / CAST(Handset_Count AS decimal(8,2)) AS decimal(8,2)) END AS HS_Pene, CASE Total_CN_NS WHEN 0 THEN 0 ELSE CAST( CAST(AR_Count_GT_45 AS decimal(8,2)) / CAST(Total_CN_NS AS decimal(8,2)) AS decimal(8,2)) END AS AR_45_Pene, CASE Total_CN_NS WHEN 0 THEN 0 ELSE CAST( CAST(AR_Count_GT_55 AS decimal(8,2)) / CAST(Total_CN_NS AS decimal(8,2)) AS decimal(8,2)) END AS AR55_Pene, CASE Number_Of_NS WHEN 0 THEN 0 ELSE CAST( CAST(TwoYR_Count AS decimal(8,2)) / CAST(Number_Of_NS AS decimal(8,2)) AS decimal(8,2)) END Two_YR_Pene, Handset_Count AS HC, Total_CN_NS AS TCN, Handset_Count_GT_79 AS HCGT79, AR_Count_GT_45 AS ACGT45, AR_Count_GT_55 AS ACGT55, TwoYR_Count AS TYC, Number_Of_NData AS N_NDATA, Acc_Revenue AS Acc_Rev, Acc_ComboPack AS Acc_CP FROM ( SELECT tblMonthlyReporting_Sups.Sup_FName + ' ' + tblMonthlyReporting_Sups.Sup_LName AS Sup_Name, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 2 THEN 1 ELSE 0 END) AS Number_Of_CN, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 1 THEN 1 ELSE 0 END) AS Number_Of_NS, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 4 THEN 1 ELSE 0 END) AS Number_Of_PP, SUM(CASE WHEN tblTransactions.Sales_Type_ID <> 4 THEN Data_Feature_Count ELSE 0 END) AS Number_Of_Data, SUM(CASE WHEN tblTransactions.Sales_Type_ID <> 4 THEN NData_Feature_Count ELSE 0 END) AS Number_Of_NData, SUM(CASE WHEN tblTransactions.Sales_Type_ID = 1 THEN 1 WHEN tblTransactions.Sales_Type_ID = 2 THEN 1 WHEN tblTransactions.Sales_Type_ID = 4 THEN 1 ELSE 0 END) AS Total_Sales, IsNull(SUM(Acc_Quantity), 0) AS Acc_Count, SUM(CASE WHEN Combo_Pack = 1 THEN 1 ELSE 0 END) AS Acc_ComboPack, IsNull(SUM(Acc_Revenue), 0) AS Acc_Revenue, SUM(CASE WHEN tblTransactions.Handset_Revenue <> 0 AND Service_Only = 0 THEN 1 ELSE 0 END) AS Handset_Count, SUM(CASE WHEN tblTransactions.Handset_Revenue >= 79.99 AND Service_Only = 0 THEN 1 ELSE 0 END) AS Handset_Count_GT_79, SUM(CASE WHEN tblTransactions.Access_Rate >= 45 THEN 1 ELSE 0 END) AS AR_Count_GT_45, SUM(CASE WHEN tblTransactions.Sales_Type_ID BETWEEN 1 AND 2 THEN 1 ELSE 0 END) AS Total_CN_NS, SUM(CASE WHEN tblTransactions.Access_Rate >= 55 THEN 1 ELSE 0 END) AS AR_Count_GT_55, SUM(CASE WHEN tblTransactions.Contract_Length = 2 AND tblTransactions.Sales_Type_ID = 1 THEN 1 ELSE 0 END) AS TwoYR_Count FROM tblMonthlyReporting_Sups LEFT JOIN tblMonthlyReporting_Reps ON tblMonthlyReporting_Sups.Sup_ID = tblMonthlyReporting_Reps.Sup_ID AND tblMonthlyReporting_Reps.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Reps.Term = 0 AND tblMonthlyReporting_Reps.Active = 1 LEFT JOIN tblTransactions ON tblMonthlyReporting_Reps.Session_ID = tblTransactions.Session_ID AND CONVERT(varchar, DATEADD(day, (-CAST(DAY(tblTransactions.Order_Date) AS int)+1), tblTransactions.Order_Date), 101) = @varReport_Month AND tblTransactions.Active = 1 WHERE tblMonthlyReporting_Sups.Mgr_ID = @varMgr_ID AND tblMonthlyReporting_Sups.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Sups.Term = 0 AND tblMonthlyReporting_Sups.Active = 1 GROUP BY tblMonthlyReporting_Sups.Sup_LName, dbo.tblMonthlyReporting_Sups.Sup_FName ) AS QT1 JOIN ( SELECT IQT1.Sup_Name, IsNull(SUM(IQT1.Gross_Acts_Quota), 0) AS Monthly_Goal FROM( SELECT tblMonthlyReporting_Sups.Sup_FName + ' ' + tblMonthlyReporting_Sups.Sup_LName AS Sup_Name, tblQuotas.Gross_Acts_Quota FROM tblMonthlyReporting_Sups LEFT JOIN tblMonthlyReporting_Reps ON tblMonthlyReporting_Sups.Sup_ID = tblMonthlyReporting_Reps.Sup_ID AND tblMonthlyReporting_Reps.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Reps.Term = 0 AND tblMonthlyReporting_Reps.Active = 1 LEFT JOIN tblQuotas ON tblMonthlyReporting_Reps.Session_ID = tblQuotas.Session_ID AND tblQuotas.Quota_Effective_Date = @varReport_Month WHERE tblMonthlyReporting_Sups.Mgr_ID = @varMgr_ID AND tblMonthlyReporting_Sups.Reporting_Month = @varReport_Month AND tblMonthlyReporting_Sups.Term = 0 AND tblMonthlyReporting_Sups.Active <> 0 GROUP BY tblMonthlyReporting_Reps.Session_ID, tblMonthlyReporting_Sups.Sup_LName, tblMonthlyReporting_Sups.Sup_FName, tblQuotas.Gross_Acts_Quota ) AS IQT1 GROUP BY IQT1.Sup_Name ) AS QT2 on QT2.Sup_Name = QT1.Sup_Name) AS Center_Totals FETCH NEXT FROM MgrList_Cursor INTO @MgrID_List, @MgrFName_List, @MgrLName_List ENDCLOSE MgrList_CursorDEALLOCATE MgrList_Cursor SET NOCOUNT OFFEND It's a little long and some the code needs to be cleaned up, i.e. top 100 percent is really not needed.It's runs extremely slow obviously. But it works. I looking for some advice and maybe some help with re-writing this.Thanks in advance. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-21 : 14:54:38
|
| In order for us to help, we would need the DDL for your tables and DML for sample data plus the expected result set. The data does not have to be real data but it does need to represent what you are trying to do. DDL is CREATE TABLE statements, DML would be INSERT INTO statements. Without that, there isn't much we can do due to the complexity of your code.Tara |
 |
|
|
|
|
|
|
|