Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 request on a query to capture the cell area a given x,y coordinate would be found in? Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, September 09, 2013 1:41 PM
 Grasshopper Group: General Forum Members Last Login: Thursday, September 12, 2013 7:29 AM Points: 14, Visits: 41
 Hi, I have a problem I would like to solve that I believe is possible with SQL query although I don't know if it would require use of advanced functions.If we look at the diagram below (pardon me for the poor translation to pure text). We can see that I have attempted to show 4 cells: A1,A2,B1,B2.If each cell is a square with the four points of a square each having an x,y coordinate it would be depicted below. All 4 cells are touching each other exactly adjacently.x,y x,y 0,0 100,0 101,0 200,0 A1 A2 0,100 100,100 101,100 200,1000,101 100,101 101,101 200,101 B1 B2 0,200 100,200 101,200 200,200 With this in place if I translate this to a database table as suchplease excuse the poor off setting of the field headers.)Upperleft_X Upperleft_Y Upperright X Upperright_Y Lowerleft_X Lowerleft_Y Lowerright_X Lowerright_Y Cell0 0 100 0 0 100 100 100 A10 101 100 101 0 200 100 200 B1101 0 200 0 101 100 200 100 A2101 101 200 101 101 200 200 200 B2This table shows that record 1 for cell A1 has 4 x,y points.My question is if I were to provide an X input and a Y input where X is between 0 and 200 and the Y input is between 0 and 200 what would the query be to be able to return the value of the Cell Field:for example: x= 25 and y = 130 would return a result of : A2another example: x= 105 and Y = 40 would return a result of : B1Is this even possible in just SQL server? How would this query scale to say a table with 10,000 recordsThe parameters would be to about 8 precision points so that we are looking at using > or >= operators to get the X and Y parameters to fall within the contrained cell areas.If not possible with SQL server, can you suggest another programming framework to use or combination there of?Any response or even the actual working SQL query would be greatly appreciated.thanksHaggisns
Post #1492908
 Posted Monday, September 09, 2013 3:46 PM
 SSCommitted Group: General Forum Members Last Login: 2 days ago @ 5:13 PM Points: 1,629, Visits: 5,114
Post #1492933
 Posted Tuesday, September 10, 2013 5:13 AM
 Right there with Babe Group: General Forum Members Last Login: Monday, February 24, 2014 12:48 AM Points: 739, Visits: 2,448
 This sounds like a job for the Geometry data type and spatial functions. The following query works for your example.`IF OBJECT_ID ('dbo.SpatialTable') IS NOT NULL DROP TABLE dbo.SpatialTable;GOCREATE TABLE SpatialTable (Cell CHAR(2), GeomCol GEOMETRY);GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('A1', 'POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0))');GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('A2', 'POLYGON ((101 0, 200 0, 200 100, 101 100, 101 0))');GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('B1', 'POLYGON ((0 101, 100 101, 100 200, 0 200, 0 101))');GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('B2', 'POLYGON ((101 101, 200 101, 200 200, 101 200, 101 101))');GODECLARE @Point GEOMETRY;SET @Point = 'POINT (25 130)'SELECT CellFROM SpatialTableWHERE GeomCol.STIntersects(@Point) = 1;SET @Point = 'POINT (105 40)'SELECT CellFROM SpatialTableWHERE GeomCol.STIntersects(@Point) = 1;` The SQL Guy @ blogspot@SeanPearceSQLAbout Me
Post #1493107
 Posted Tuesday, September 10, 2013 8:32 AM
 Grasshopper Group: General Forum Members Last Login: Thursday, September 12, 2013 7:29 AM Points: 14, Visits: 41
 thanks The SQL Guy,I am still installing SQL Server 2008 and SSMS to see how this all works out.I will endeavor to post properly in the future.
Post #1493213
 Posted Wednesday, September 11, 2013 7:14 AM
 Right there with Babe Group: General Forum Members Last Login: Monday, February 24, 2014 12:48 AM Points: 739, Visits: 2,448
 My apologies for posting a Spatial answer in the 2005 forum. I must learn to read properly To answer your question for the 2005 platform I must ask a question. Are the shapes always square or are they polygons? The SQL Guy @ blogspot@SeanPearceSQLAbout Me
Post #1493661
 Posted Wednesday, September 11, 2013 12:15 PM
 Grasshopper Group: General Forum Members Last Login: Thursday, September 12, 2013 7:29 AM Points: 14, Visits: 41
 Yes, they are always squares.
Post #1493841
 Posted Wednesday, September 11, 2013 9:12 PM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 7:25 PM Points: 3,534, Visits: 4,962
 If they're always squares, perhaps something like this?`WITH Squares ( Upperleft_X, Upperleft_Y, Upperright_X, Upperright_Y ,Lowerleft_X, Lowerleft_Y, Lowerright_X, Lowerright_Y, Cell) AS ( SELECT 0, 0, 100, 0, 0, 100, 100, 100, 'A1' UNION ALL SELECT 0, 101, 100, 101, 0, 200, 100, 200, 'B1' UNION ALL SELECT 101, 0, 200, 0, 101, 100, 200, 100, 'A2' UNION ALL SELECT 101, 101, 200, 101, 101, 200, 200, 200, 'B2'),Points (n, x, y) AS ( SELECT 1, 25, 130 UNION ALL SELECT 2, 105, 40)SELECT a.n, b.Cell, x, y, Upperleft_X, Upperright_X, Lowerleft_X, Lowerright_X , Upperleft_Y, Upperright_Y, Lowerleft_Y, Lowerright_YFROM Points aJOIN Squares b ON (x BETWEEN Upperleft_X AND Lowerright_X AND y BETWEEN Upperleft_Y AND Lowerright_Y); ` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1493981
 Posted Thursday, September 12, 2013 2:22 AM
 Right there with Babe Group: General Forum Members Last Login: Monday, February 24, 2014 12:48 AM Points: 739, Visits: 2,448
 Hi,If you are only dealing with squares it is quite easy.Firstly you only need the top left and bottom right co-ordinates:`CREATE TABLE Squares (CellNo CHAR(2) NOT NULL, TopLeftX INT NOT NULL, TopLeftY INT NOT NULL, BottomRightX INT NOT NULL, BottomRightY INT NOT NULL);INSERT INTO Squares VALUES ('A1', 0, 0, 100, 100), ('A2', 0, 101, 100, 200), ('B1', 101, 0, 200, 100), ('B2', 101, 101, 200, 200);`Secondly, to make it scale to 10,000+ records you need to index the four co-ordinates. I would cluster the table on these columns:`ALTER TABLE SquaresADD CONSTRAINT SquaresCoordinates UNIQUE CLUSTERED (TopLeftX, TopLeftY, BottomRightX, BottomRightY);`Lastly, you just need to query for any values where the X and the Y each falls between the upper left and bottom right range. You can use a join as Dwain showed you above or you can specify the points in a where clause.`SELECT *FROM SquaresWHERE (25 BETWEEN TopLeftX AND BottomRightX AND 130 BETWEEN TopLeftY AND BottomRightY)OR (105 BETWEEN TopLeftX AND BottomRightX AND 40 BETWEEN TopLeftY AND BottomRightY);` The SQL Guy @ blogspot@SeanPearceSQLAbout Me
Post #1494048

 Permissions