Solved

MS SQL Query De-Mystified

Posted on 2009-07-13
2
190 Views
Last Modified: 2012-05-07
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;
0
Comment
Question by:Jason Jones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 500 total points
ID: 24841937
Pseudo code:

if p.UnitAssessment = -1 then
   dblBenefit = p.UnitAssessmentValue
else
   if (p.BuildHT >= 4) then
        dblBenefit = (P.CTaxLand*(P.Benef*0.01))+((P.CTaxBuild/P.BuildHT)*3*(P.Benef*0.01)) +
                        ((P.CTaxBuild/P.BuildHT)*(P.BuildHT-3)*0.06)
   else
       if P.Partial_Prot = -1 then
             if P.Bldg_Incl = -1 then
                   dblBenefit = (P.CTaxLand*(P.Wet_Perc*0.01)*0.03)+(P.CTaxBuild*0.03)
             else
                   dblBenefit = P.CTaxLand*(P.Wet_Perc*0.01)*0.03)
             end if
       else
             dblBenefit = (P.CTaxLand+P.CTaxBuild)*(P.Benef*0.01)
   end if
end if
0
 
LVL 4

Expert Comment

by:j_s_kelley
ID: 24842301
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

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question