Link to home
Start Free TrialLog in
Avatar of Kristen Jones
Kristen JonesFlag for United States of America

asked on

MS SQL Query De-Mystified

Can some one translate this query so it can be understood as to what it is doing?  Feel free to explain it as if you were talking to a Monkey or 3 year old


SELECT P.County_ID, P.Parcel_ID, P.Rate_ID, P.Add_ID, P.Exempt, IIf(P.UnitAssessment=-1,P.UnitAssessmentValue,IIf(P.BuildHT>=4,(P.CTaxLand*(P.Benef*0.01))+((P.CTaxBuild/P.BuildHT)*3*(P.Benef*0.01))+((P.CTaxBuild/P.BuildHT)*(P.BuildHT-3)*0.06),IIf(P.Partial_Prot=-1,IIf(P.Bldg_Incl=-1,(P.CTaxLand*(P.Wet_Perc*0.01)*0.03)+(P.CTaxBuild*0.03),P.CTaxLand*(P.Wet_Perc*0.01)*0.03),(P.CTaxLand+P.CTaxBuild)*(P.Benef*0.01)))) AS dblBenefit, BR.Bond_rate, TR.Tax_Rate, P.id, P.UnitAssessment, P.UnitAssessmentValue, TR.TaxRate_Description, P.MainCounty
FROM BondRate AS BR, MATCHEDTAXPARCELS AS P INNER JOIN TaxRate AS TR ON P.Rate_ID=TR.CharCode;
ASKER CERTIFIED SOLUTION
Avatar of mohan_sekar
mohan_sekar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of j_s_kelley
j_s_kelley

The best way to understand a query is to actually format it.  Once I did that, I could see better.  Without understanding the exact nature of the query, the vast majority of it is straightforward.  It is a join where most of the fields are selected.  the field dblBenefit looks like it has some serious logic to determine it's value.  That would take an understanding of the domain to do it justice.  Also, one thing I did notice is that there are 3 tables but only two of them are joined.  BondRate is not joined with anything.  You have MatchedTaxParcels joined with Tax Rate and the result of that will be FULL OUTER JOINED with BondRate.  This is more than likely a mistake.
SELECT P.County_ID, 
	P.Parcel_ID, 
	P.Rate_ID, 
	P.Add_ID, 
	P.Exempt, 
	IIf(P.UnitAssessment=-1,P.UnitAssessmentValue,
			IIf(P.BuildHT>=4,(P.CTaxLand*(P.Benef*0.01))+((P.CTaxBuild/P.BuildHT)*3*(P.Benef*0.01))+((P.CTaxBuild/P.BuildHT)*(P.BuildHT-3)*0.06),
				IIf(P.Partial_Prot=-1,IIf(P.Bldg_Incl=-1,(P.CTaxLand*(P.Wet_Perc*0.01)*0.03)+(P.CTaxBuild*0.03),P.CTaxLand*(P.Wet_Perc*0.01)*0.03),(P.CTaxLand+P.CTaxBuild)*(P.Benef*0.01)))) AS dblBenefit, 
	BR.Bond_rate, 
	TR.Tax_Rate, 
	P.id, 
	P.UnitAssessment, 
	P.UnitAssessmentValue, 
	TR.TaxRate_Description, 
	P.MainCounty
FROM BondRate AS BR, MATCHEDTAXPARCELS AS P 
	INNER JOIN TaxRate AS TR ON P.Rate_ID=TR.CharCode

Open in new window