March 20, 2007 at 7:46 am
i need help on this.when i run this proc i am getting duplicates.
set
ANSI_NULLS ON
set
QUOTED_IDENTIFIER ON
go
create Procedure [dbo].[COSTGetCSIProjectPhase] (@MSBA_ID varchar(10), @Project_Phase_ID int)
AS
Declare
@Total_CSICost int,
int
-- Get GSF
Select
@gsf = Gross_Square_Footage
From
COST_Project_Phase
Where
MSBA_ID = @MSBA_ID
And
Project_Phase_ID = @Project_Phase_ID
If
@gsf is Null
Set @gsf = 0
-- Get the rollups
Select
ACC.Rollup_To_Class, ACC.Rollup_To_Category, ACC.Rollup_To_Phase, Sum(CPPC.CSI_Cost) CSI_Cost
Into
#tmp_Level
From
APPN_Classification_Category ACC,
COST_Project_Phase_CSI CPPC
Where
ACC.CSI_Flag = 1
And ACC.Classification_ID = CPPC.Classification_ID
And CPPC.MSBA_ID = @MSBA_ID
And CPPC.Project_Phase_ID = @Project_Phase_ID
Group
By ACC.Rollup_To_Class, ACC.Rollup_To_Category, ACC.Rollup_To_Phase
-- Get Total CSI.
Select
@Total_CSICost = COALESCE(Sum(CSI_Cost),0)
From
#tmp_Level
-- Level 1
Select
Rollup_To_Class, Sum(CSI_Cost) CSI_Cost
Into
#tmp_level1
From
#tmp_Level
Group
By Rollup_To_Class
-- Level 2
Select
Rollup_To_Category, Sum(CSI_Cost) CSI_Cost
Into
#tmp_level2
From
#tmp_Level
Group
By Rollup_To_Category
-- 1st level data.
Select
ACC
.Classification_ID,
ACC
.Classification_Code,
ACC
.Classification_Name,
ACC
.Classification_Level,
ACC
.Allow_To_Set_Budget,
Case When ACC.Classification_Level = 3 Then ACC.Rollup_To_Category
When ACC.Classification_Level = 2 Then ACC.Rollup_To_Class
When ACC.Classification_Level = 1 Then '0'
When ACC.Classification_Level = 4 Then ACC.Rollup_To_Phase End Parent_Classification_Code,
COALESCE(CPPC.CSI_Cost, 0) CSI_Cost,
Case When @Total_CSICost = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@Total_CSICost End Distribution_Percent,
Case When @gsf = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@gsf End Cost_per_SQFT
From
APPN_Classification_Category ACC
Left
Outer Join #tmp_Level1 CPPC
On
ACC.Classification_Code = CPPC.Rollup_To_Class
Where
ACC.CSI_Flag = 1
And Classification_Level = 1
Union
-- 2nd level data.
Select
ACC
.Classification_ID,
ACC
.Classification_Code,
ACC
.Classification_Name,
ACC
.Classification_Level,
ACC
.Allow_To_Set_Budget,
Case When ACC.Classification_Level = 3 Then ACC.Rollup_To_Category
When ACC.Classification_Level = 2 Then ACC.Rollup_To_Class
When ACC.Classification_Level = 1 Then '0'
When ACC.Classification_Level = 4 Then ACC.Rollup_To_Phase End Parent_Classification_Code,
COALESCE(CPPC.CSI_Cost, 0) CSI_Cost,
Case When @Total_CSICost = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@Total_CSICost End Distribution_Percent,
Case When @gsf = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@gsf End Cost_per_SQFT
From
APPN_Classification_Category ACC
Left
Outer Join #tmp_Level2 CPPC
On
ACC.Classification_Code = CPPC.Rollup_To_Category
Where
ACC.CSI_Flag = 1
And Classification_Level = 2
Union
-- 3rd level data.
Select
ACC
.Classification_ID,
ACC
.Classification_Code,
ACC
.Classification_Name,
ACC
.Classification_Level,
ACC
.Allow_To_Set_Budget,
Case When ACC.Classification_Level = 3 Then ACC.Rollup_To_Category
When ACC.Classification_Level = 2 Then ACC.Rollup_To_Class
When ACC.Classification_Level = 1 Then '0'
When ACC.Classification_Level = 4 Then ACC.Rollup_To_Phase End Parent_Classification_Code,
COALESCE(CPPC.CSI_Cost, 0) CSI_Cost,
Case When @Total_CSICost = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@Total_CSICost End Distribution_Percent,
Case When @gsf = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@gsf End Cost_per_SQFT
From
APPN_Classification_Category ACC
Left
Outer Join #tmp_Level CPPC
On
ACC.Classification_Code = CPPC.Rollup_To_Phase
Where
ACC.CSI_Flag = 1
And Classification_Level = 3
Union
-- 4th level data.
Select
ACC
.Classification_ID,
ACC
.Classification_Code,
ACC
.Classification_Name,
ACC
.Classification_Level,
ACC
.Allow_To_Set_Budget,
Case When ACC.Classification_Level = 3 Then ACC.Rollup_To_Category
When ACC.Classification_Level = 2 Then ACC.Rollup_To_Class
When ACC.Classification_Level = 1 Then '0'
When ACC.Classification_Level = 4 Then ACC.Rollup_To_Phase End Parent_Classification_Code,
COALESCE(CPPC.CSI_Cost, 0) CSI_Cost,
Case When @Total_CSICost = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@Total_CSICost End Distribution_Percent,
Case When @gsf = 0 Then 0 Else COALESCE(CPPC.CSI_Cost, 0)/@gsf End Cost_per_SQFT
From
APPN_Classification_Category ACC
Left
Outer Join COST_Project_Phase_CSI CPPC
On
ACC.Classification_ID = CPPC.Classification_ID
And CPPC.MSBA_ID = @MSBA_ID
And CPPC.Project_Phase_ID = @Project_Phase_ID
Where
ACC.CSI_Flag = 1
And Classification_Level = 4
Union
-- Get the top level grant total data.
Select
0 Classification_ID,
'0' Classification_Code,
'Grant Total' Classification_Name,
0 Classification_Level
,
0 Allow_to_Set_Budget
,
'0' Parent_Classification_Code,
COALESCE(Sum(CSI_Cost),0) CSI_Cost,
1 Distribution_Percent
,
Case When @gsf = 0 Then 0 Else @Total_CSICost/@gsf End Cost_per_SQFT
From
#tmp_Level
Order
by 4, 2
March 20, 2007 at 9:04 am
is there any guru available to help me
March 20, 2007 at 9:37 am
March 20, 2007 at 10:54 am
Hi Best ,
Wht kind of duplicacy it is.... Do u have some sample data which u can provide...
Thanks & Regards..
Amit Gupta..
March 20, 2007 at 11:00 am
i dont have sample data but if you see the proc on all the four level where iam doing the rollup i am getting duplicate.
March 20, 2007 at 11:09 am
March 21, 2007 at 2:39 am
Sorry, Best, but you need to help us first if we are to help you. No structure, no sample data = no help. You should understand that it is impossible to pinpoint source of errors without having any idea about how the data look. We can offer some wild guess, but that's not the best way to do it and probably doesn't help you much.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply