I have a problem with an SQL query and i've narrowed it down to the following code
SELECT ACOS( (SIN(PI()* 52.9519918465976/180)*SIN(PI()* 52.9519918465976/180))+(COS(PI()* 52.9519918465976/180)*cos(PI()* 52.9519918465976/180)*COS(PI()* -1.14304013581239/180-PI()* -1.14304013581239/180))) AS test
I get the following error message 'An invalid floating point operation occurred'
Can abody see what the issue is?
Thanks in advance
Your result in ACOS() is bigger than 1 which can't be.
That is because of floating point inaccuracy. it could be 1.00000001 for instance. Putting it just a little below 1 works like this:
SELECT ACOS(
(SIN(PI()* 52.9519918465976/180.0)* SIN(PI()* 52.9519918465976/180.0))
+ (COS(PI()* 52.9519918465976/180)*cos(PI()* 52.9519918465976/180.0)*COS(PI()* -1.14304013581239/180.0-PI()* -1.14304013581239/180.0))
- 0.0000001
)
I'm using ACOS to calculate distances between geo points. The subtraction of '- 0.0000001' is enough to skew my results a bit. So I instead, but used a MIN function (as posted here by Craig) like this:
SELECT ACOS(
(SELECT MIN(x) FROM (VALUES (
(SIN(PI()* 52.9519918465976/180.0)* SIN(PI()* 52.9519918465976/180.0))
+ (COS(PI()* 52.9519918465976/180)*cos(PI()* 52.9519918465976/180.0)*COS(PI()* -1.14304013581239/180.0-PI()* -1.14304013581239/180.0))
),(1)) AS value(x))
)
This way ACOS of floats between 0 and 1 remain accurately calculated.
Related
Straight to the point and this might be very simple for some of you.
I have a simple SELECT query (select description from table) which produces all i want like below :
- testword123
- testword875
- myjob1 45
- myjob is 544
What i need is to have as a result :
- testword
- myjob
I can use a SELECT distinct LEFT(description,8) which works fine, but the problem is not ALL 'description' have the same number of words :-(
So basically, what i want is retrieve ONLY the letters from the 'description' result set.
Thanks!!
R
SELECT distinct LEFT(description, charindex(' ', description) - 1)
Depending on your implementation, it might be possible to declare 'description' as a variable beforehand so you don't have to type it twice in the same query.
There are two decisions:
1) Handle each decription in PHP
or
2) Handle user input before writing it to DB. Add field to table as index of first not letter symbol and then use it in LEFT mysql function
Thanks "undefined_variable" - Your solution "stackoverflow.com/questions/11134452/…; was the correct one!! (y) (with a little bit of tweaking, this helped A LOT) A+++
I'm trying to calculate the conversion rate of clicks and unique visitors for local analytic part of a website. MySQL 5.1.73, appears to not like my query. I'm not sure if the sources I'm getting are using deprecated syntax, or I'm simply not asking the correct question in Google.
SELECT
(SUM(click1) DIV SUM(unique) * 100) /* <<<< Synrax Error Here */
FROM
stats_clicks
WHERE
aid = 10050 AND
timestamp_local BETWEEN '2014/01/01' AND '2015/12/01'
Sorry if this is a very beginner-isk question. A lot of my resources are not working in this case.
unique is a reserved word in SQL. The best idea would probably be to rename it to unique_clicks or something down those lines. If that is not possible, you can escape it with forward quotes (`s).
Additionally, while div is perfectly legal syntax in MySQL, it performs integer division. Chances are you meant to use floating point division, which can simply be done with the / operator:
SELECT (SUM(click1) / SUM(`unique`) * 100)
Use
(100 * SUM(click1) / SUM(`unique`))
I am trying to come up with the most efficient way to resolve the following problem. I have a large table locations in which points are stored using two doubles, one for Lat and one for Lng, which are indexed, as well as a radius.
I am trying to select all locations that are within the radius of a given Latitude and Longitude. My current approach is to run a subquery that first gathers all results within a "geographic square", and then from those results, calculate the distance and determine whether it is within range like so:
Subquery (select only those within a rough mile x mile square):
SELECT * FROM events WHERE $lat +0.015 > lat AND lat > $lat-0.015 AND $lng +0.02 > lng AND lng > $lng-0.02
Determine distance via Haversine:
SELECT * FROM ( the subquery ) WHERE Radius >= ( 20903520 * acos( cos(RADIANS(Lat)) * cos(RADIANS($lat)) * cos(RADIANS(Lng - $lng)) + sin(RADIANS(Lat)) * sin(RADIANS($lat))))
My question is, what is a better way of approaching this? I'm fairly sure this is among the least efficient ways of going about this and any help is greatly appreciated.
Another solution is found here: http://mysql.rjweb.org/doc.php/latlng It is essentially O(1), whereas most index-based solutions are O(sqrt(N)).
My technique works with InnoDB. Spatial indexes require MyISAM (until 5.7).
In pseudo code I'd like to accomplish this:
select
count(groupA) as gA,
count(groupB) as gB,
count(groupC) as gC,
sum(gA,gB,gC) as groupABCTotal
But the various ways I've tried has resulted in syntax errors. What's the correct way to achieve this without re-selecting the group counts for the sum?
If you want the sum as another column of output, you'll have to re-select the constituent columns in one fashion or another.
I find this rather readable:
SELECT d.*, gA+gB+gC AS groupABCTotal
FROM (SELECT COUNT(groupA) AS gA,
COUNT(groupB) AS gB,
COUNT(groupC) AS gC
FROM tbl) d;
But this works, too, as you know:
SELECT COUNT(groupA) AS gA,
COUNT(groupB) AS gB,
COUNT(groupC) AS gC,
COUNT(groupA)+COUNT(groupB)+COUNT(groupC) AS groupABCTotal
FROM tbl;
Now, MySQL is probably smart enough not to recompute redundant aggregrates, so that COUNT(groupA) would be computed only once in the second form above.
I'm building a database where the user will be able to type in a search field for what they are looking for, and I need the results to list in a manner where those closest to the user will show first, and work their way further from the user's current location.
So I'm looking for a method to include (factor in) Geo location with a query. The database will have a multitude of fields that will be searched through, including the location of each.
This whole process will be much like that of Google's Map location searches (ie. Searching for "Restaurants near me").
I've tried googling for this answer, I've asked on other forums, and I've searched stackoverflow and have yet to find help in accomplishing this.
You can use MySQL Spatial Extensions to order results by distance
http://dev.mysql.com/doc/refman/5.5/en/spatial-extensions.html
That allows you to do queries like
SELECT name, AsText(location), SQRT(POW( ABS( X(location) - X(#center)), 2) + POW( ABS(Y(location) - Y(#center)), 2 )) AS distance
FROM Points
WHERE Intersects( location, GeomFromText(#bbox) )
AND SQRT(POW( ABS( X(location) - X(#center)), 2) + POW( ABS(Y(location) - Y(#center)), 2 )) < #radius
ORDER BY distance;
http://howto-use-mysql-spatial-ext.blogspot.com/