Receiving the following error with my query when calculating distance - sql

I'm receiving the following error "An invalid floating point operation occurred." when I run this query:
SELECT PolID, LocID, Address, City, StateCode, OrigGeoLat, OrigGeoLong, NewGeoLat, NewGeoLong,
acos(sin(radians(OrigGeoLat)) * sin(radians(NewGeoLat)) +
cos(radians(OrigGeoLat)) * cos(radians(NewGeoLat)) *
cos(radians(OrigGeoLong - NewGeoLong))) * 6372.8 as Distance
FROM zzGeoDataTMP
All of the *geoLat and *geoLong data is defined as numeric(18,10). When I run the query I start getting data back and then I get errors on specific rows of data. For example the following row throws the above mentioned exception only once the ACOS function is called:
OrigGeoLat|OrigGeoLong|NewGeoLat|NewGeoLong
---------------------------------------------
32.9364620|-80.0411000|32.9364620|-80.0411000
Thanks so much in advance for any insight you may be able to help with!

You're calculation is accumulating some small errors, which mean that the calculated value (passed to ACOS) is slightly higher than 1. Try wrapping it with a ROUND call:
acos(ROUND(sin(radians(OrigGeoLat)) * sin(radians(NewGeoLat)) +
cos(radians(OrigGeoLat)) * cos(radians(NewGeoLat)) *
cos(radians(OrigGeoLong - NewGeoLong)),15)
)
Where we're only keeping 15 decimal places of accuracy.

Related

Using an if-else statement in big-query with a IFNULL statement

I have a delicate dilemma with follwing SQL statement in Bigquery:
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice))) AS TotalCost,
I need to change it to check if some articles are present or not and if they are, I have to do one expressiona and if not another expression.This is my check
IF(a.ArticleNumber NOT IN ("204","204.2","204.3","204.4","204.5","204.6","204.7")
If those articles are not present then I have to run following query:
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice)))
Otherwise ( and here is where the unfortunate part comes in I guess ) I have to run something like this:
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice) / (a.SalesPrice * i.CurrencyRate)))
So the whole code snippet I have so far is:
IF(a.ArticleNumber NOT IN ("204","204.2","204.3","204.4","204.5","204.6","204.7"),
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice))),
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice) / (a.SalesPrice * i.CurrencyRate)))
) AS TG,
The result is a error saying:
division by zero: 0 / 0
The IFNULL I have to run unfortunately since my users have not be consistent in their data input. But even when only using one of either choices inside the IFNULL, I get the same response saying division by zero: 0/0
Endresult is that IF certaina rticles are present, then I need to get those including the i.CurrencyRate, otherwise without i.CurrencyRate. Any ideas?
PS! I followed this tutorial
IF(a.ArticleNumber NOT IN ("204","204.2","204.3","204.4","204.5","204.6","204.7"),
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice))),
ROUND(SUM(SAFE_DIVIDE((r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice)) , (a.SalesPrice * i.CurrencyRate))))
) AS TG,

U-sql error: Expected one of: AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')' ','

I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.

Trying to mutliply the result of a column and a number but keep getting error in oracle

i am trying to create a dynamic action that calculates the commission of an estate agent by multiplying the asking price by the commission rate 6%. but i keep getting an error in oracle. i am a beginner to oracle. any help will bee appreciated.
:P13_ASKING_PRICE * 0.06
this is the code i put which multiplies the asking price by 0.06
but i get this error
ORA-06550: line 1, column 145: PLS-00103: Encountered the symbol "*"
when expecting one of the following: := . ( # % ; indicator
What kind of a dynamic action was it? Apex expects
:P13_RESULT := :P13_ASKING_PRICE * 0.06;
not just
:P13_ASKING_PRICE * 0.06
[EDIT]
I've tested it right now; I created two items on a page, P13_ASKING_PRICE which expects a value to be entered, and P13_RESULT which will display the result.
"Set value" dynamic action on P13_ASKING_PRICE item
Set type: "PL/SQL Function body"
PL/SQL Function body: return (:P13_ASKING_PRICE * 0.06);
Items to submit: P13_ASKING_PRICE
Affected elements:
selection type: item(s)
item(s): P13_RESULT
Fire on initialization: No
That's all; works well, so - try it.

An invalid floating point operation occurred when checking longitude and latitudes

I know there are a lot of questions like mine asked. I looked at them, but I can't seem to resolve the problem. I'm just bad in math :s.
In c# i'm using a SqlQuery to get cities in a range. This works most of the time, except for some cities.
For example, if i ask the nearby cities of the city with postalcode 2060. I get this error. When i ask it for the city with code 2000, it returns 2000 and 2060.
2060: Lat = 51.2293515000 Long = 4.4279883000
2000: Lat = 51.2198771000 Long = 4.4011356000
this is the query:
return base.Database.Database.SqlQuery<City>(
"SELECT * FROM [dbo].[City] WHERE #p0 >= (((acos(sin((#p1*pi()/180)) *
sin(([Latitude]*pi()/180))+cos((#p1*pi()/180)) * cos(([Latitude]*pi()/180)) *
cos(((#p2- [Longitude])*pi()/180))))*180/pi())*60*1.1515*1.609344)"
, radius, latitude, longitude);
Can somebody explain how this can be changed so it works for all the "cities" and the reason if this error for a noob in math?
Thank you
The problem is that the statement inside acos() gets a rounding error somtimes when you compare cities to themselves, and the statement becomes slightly over 1, which causes the error.
You need to cap the value to max 1 somehow. Here is a rather ugly solution:
return base.Database.Database.SqlQuery<City>(
"SELECT * FROM [dbo].[City] WHERE #p0 >= (((acos((select min(v) from (values (1), (sin((#p1*pi()/180)) *
sin(([Latitude]*pi()/180))+cos((#p1*pi()/180)) * cos(([Latitude]*pi()/180)) *
cos(((#p2- [Longitude])*pi()/180)))) as x(v))))*180/pi())*60*1.1515*1.609344)"
, radius, latitude, longitude);
I'm not sure, but it could be that it sometimes gets slightly smaller than -1 as well, which would case the same error. Then you'd have to cap the value to between -1 and 1.

Visual Studio error on valid SQL

I'm getting an annoying error in visual studio for SQL that executes fine.
SELECT InvoiceLines.LineID,
InvoiceLines.InvoiceID,
InvoiceLines.Text,
InvoiceLines.Rate,
InvoiceLines.Count,
InvoiceLines.Rate * InvoiceLines.Count AS LineTotal,
((InvoiceLines.Rate * InvoiceLines.Count) * (1 + Invoices.VatRate / 100)) * (1 - CAST(Invoices.Discount AS money) * InvoiceLines.ApplyDiscount / 100) AS LineTotalIncVat, InvoiceLines.ApplyDiscount
FROM InvoiceLines
LEFT JOIN Invoices ON Invoices.InvoiceID = InvoiceLines.InvoiceID
What LineTotalIncVat is trying to do is compute the total for the invoice item while adding the vat and subtracting the discount, and yes, probably better to do this in code (would if I could)
The error visual studio gives is:
There was an error parsing the query [token line number =1, token line offset =14, token in error = InvoiceLines]
Even though it will validate and execute without a problem in the query builder...
Try bracketing the table and column names, it may be having trouble parsing InvoiceLines.Count because Count is a reserved word. Try [InvoiceLines].[Count].
Solved
Deleted the table from the dataset and added it again with exactly the same SQL.
How strange... although not the first time I've had to do this.