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 2000 Forums
 SQL Server Development (2000)
 Calculation query based on existence of data in multiple fields

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-05-02 : 09:14:49
Sherry writes "How do I write a query that will calculate a value based an assigned hierarchy to value fields in a record. The case statement only seems to work on one field. I need to check 5 fields in a certain priority to calculate correctly. I figured a way to do it with a stored procedure creating a cursor and a table, but man it is ugly.


Here is a snipit of the stored procedure: I left out the create table stuff at the top.

Any help is appreciated!

DECLARE stv_by_county CURSOR
FOR
SELECT Parcel_info.parcel_info_id,Parcel_Info.County_Id, Parcel_Values.Tax_Year, Final_Resolved_Value, Final_Stipulated_Value,
Stipulated_Value,Final_Assessed_Value, Assessed_Value, CLR_value
FROM Ratio INNER JOIN (Parcel_Info INNER JOIN Parcel_Values ON Parcel_Info.Parcel_Info_Id = Parcel_Values.Parcel_Info_Id) ON (Ratio.County_id = parcel_info.county_id)
AND (Ratio.tax_Year = Parcel_Values.Tax_Year)
WHERE (((Parcel_Info.Tax_Type)=1));

DECLARE @county_id as char(6), @parcel_info_id as int, @tax_year as char(4),
@final_stip as decimal (18,2), @Stipulated as decimal (18,2), @final_assessed as decimal (18,2),
@assessed as decimal (18,2), @clr as decimal (8,2), @state_taxable as decimal (18,2), @final_resolved as decimal (18,2)

set @state_taxable = 0

OPEN stv_by_county

FETCH stv_by_county INTO @parcel_info_id, @county_id, @tax_year, @final_resolved, @final_stip, @Stipulated, @final_assessed,
@assessed, @clr
WHILE (@@FETCH_STATUS = 0)
BEGIN
IF @final_resolved IS NOT NULL
BEGIN
set @state_taxable = @final_resolved * @clr
END
ELSE IF @final_stip IS NOT NULL
BEGIN
set @state_taxable = @final_stip
END
ELSE IF @stipulated IS NOT NULL
BEGIN
set @state_taxable = @stipulated
END
ELSE IF @final_assessed IS NOT NULL
BEGIN
set @state_taxable = @final_assessed * @clr
END
ELSE set @state_taxable= @assessed *@clr

insert stv_parcel_values( parcel_info_id, tax_year, stv) values (@parcel_info_id,@tax_year,@state_taxable)

FETCH stv_by_county INTO @parcel_info_id, @county_id, @tax_year, @final_resolved, @final_stip, @Stipulated, @final_assessed, @assessed, @clr
END
"
   

- Advertisement -