MySQL - Find Closest Match in range of numbers - mysql

I'm working on a graph type application and I'm looking to find the best solution within a x,y axis start to end point.
MySQL Data:
Say I have the following:
starting_x: 200
starting_y: 150
ending_x: 500
ending_y: 605
So I'm wanting to find the closest match between the above numbers to the database.
My query I'm working with now:
SELECT * FROM `graph` ORDER BY `start_pos_x`,`start_pos_y`,`end_pos_x`,`end_pos_y` ASC
I know this is not even close to what I'm trying to do but i'm having a hard time finding a solution on here.
Thanks!

This seems to be an algorithm question.
First, you must define the closest match between the above numbers to the database. For example, I can define it as to find "the lowest sum of the squares of the differences between Xs and of Ys."
Then, the solution might be something like:
Select
*
From
`graph`
Order by
Power(starting_x-200, 2) + Power(starting_y-150, 2)
+ Power(ending_x-500, 2) + Power(ending_y-605, 2) ASC

Related

Sorting of numbers gives a random order in cross table

I am having an issue with a cross table that I would like to sort. On the vertical axis I have some categories, and on the horizontal axis I have time. The table itself is filled with the value that is related to the previous two. I created a rank, which seems to be correct, but when I sort that rank, I seem to get a random order of numbers (example is the upper 10 for ascending order):
4971 - 420 - 4959 - 1 - 4969 - 4970 - 16 - 2 - 7 - 420
The value I ranked is somewhat odd:
=Last([X]) - Last(Previous([X]))
Where X is a measurement.
I already checked if I am using other sortings that I might have overlooked, but this is not the case. Also I am sure that this ranking is a number. I use merged items for the categories, and I used the combines object instead of one of the 'childs'.
Can someone tell me what I am overlooking?
I am on BO 2016, SP2.
The issue is solved by making a variable Delta which is =abs(Previous[X]-X). I also made a new variable that shows the current time, and I sort on Delta where the current time is equal to the time in the last column.

Spatialite find the nearest node for the given lat/lon

I am struggle on this for a few days, I am not sure why the query was wrong..
I want to find the nearest point at the given lat/lon.
SELECT rid,DISTANCE(geometry, MakePoint(-79.91759, 43.266571))
FROM room2f ORDER BY DISTANCE(Startpoint(geometry), MakePoint(-79.91759, 43.266571))limit 1
It's always return the first row, nomatter what point I use. Then I tried to remove "limit1":
SELECT rid,DISTANCE(geometry, MakePoint(-79.91759, 43.266571))
FROM room2f ORDER BY DISTANCE(Startpoint(geometry), MakePoint(-79.91759, 43.266571))
The result is very strange... The result is sorted by rid rather than distance, I think this is why it always return the first row. Is my query wrong? Or, there is a bug in Spatialite?
Thanks]1
In your sql statement you compare to different distances..
From your geometry in general to your specific point (as I know it is the shortest distance from the point to your geometry).
From the first point of your geometry to your specific point.
Therefore you also get a different ordering of your result!
See the reference to the distance function:
http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.0.html#p13

Access query using calculation

I'm trying to make a query using a calculation with Date().
I have a field named [Currentordue] and a field named [duedate]. What I'm trying to accomplish is making a query to limit the results by "if [currentordue] equals "due" or if [duedate] minus today's date is less than 30 days."
I've tried a few different ways but always seem to end with either an error or with no results showing (which would be an error as well since I know there are fields that are due).
Any and all help would be appreciated.
Here is a way to use two different date conditions:
SELECT Table1.Currentordue, Table1.duedate, DateDiff("d",[duedate],Date()) AS Expr1
FROM Table1
WHERE (((DateDiff("d",[duedate],Date()))<30)) OR (((Table1.Currentordue)=[duedate]));

get difference between 2 dates in google script

I'm trying to get the difference between the 2 dates in googlescript. However it doesn't seem to be working. Does anyone seem to know why? My user defined variables are working it's just when i get the difference between the 2 the result is like 4.28E8 whereas the answer is supposed to be like 40. Is there a way to convert the date into a number then convert it back into a date?
ddate = output.getRange(lRow2,2,1,1).getValue() - output.getRange(2,1,1,1).getValue()
Function works, its just the difference is in milliseconds. For those interested in the conversion, it's as follows:
diff = ((output.getRange(lRow2,2,1,1).getValue() - output.getRange(2,1,1,1).getValue()) / (1000*60*60*24))
or simply:
days = milliseconds/ (1000*60*60*24)

grab mysql result based on field values

I am taking over designing a CMS from another programmer. As the site is filling up, we're finding loops in mysql queries causing long hangs. I have found a temp solution for this one, but am wondering if there is a quicker way of doing it?
take the table (tracks resources):
id resource click
1 res_1 192
2 res_2 12
3 res_3 300
what we need to get is a popularity of the resource - res_click/total_click
what he had was a while loop:
while ($item = mysql_fetch_array ($result)) $total_clicks = $total_clicks + $item[0];
As there could be 100 or more resources to a page, this was running for each resource, and it is causing major hangs.
My solution is to get a sum:
SELECT SUM(click) FROM uri
SELECT click FROM resource WHERE id=$x
then divide them both.
But this two calls are still running for around a 100 items per page. Is there a way I can have a field in mysql that is the result of a formula based on another another, like in excell? So I could add a field "percentage", tell mysql that it is the sum of click divided by the current click value, then every time click is updated the 'percentage' field is automatically updated?
any help would be appreciated,
cheers ;)
you can create a view on your table that present the sum you want