SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


request on a query to capture the cell area a given x,y coordinate would be found in?


request on a query to capture the cell area a given x,y coordinate would be found in?

Author
Message
haggisns
haggisns
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 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,100

0,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 suchSadplease 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 Cell
0 0 100 0 0 100 100 100 A1
0 101 100 101 0 200 100 200 B1
101 0 200 0 101 100 200 100 A2
101 101 200 101 101 200 200 200 B2

This 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 : A2
another example: x= 105 and Y = 40 would return a result of : B1

Is this even possible in just SQL server? How would this query scale to say a table with 10,000 records

The 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.

thanks

Haggisns
mister.magoo
mister.magoo
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4112 Visits: 7865
Hi,

Please read this article and post some sample data that can easily be used.


You will get help much quicker with this information.

Forum Etiquette: How to post data/code on a forum to get the best help

MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Sean Pearce
    Sean Pearce
    SSCommitted
    SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

    Group: General Forum Members
    Points: 1770 Visits: 3432
    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;
    GO

    CREATE TABLE SpatialTable
    (Cell CHAR(2),
    GeomCol GEOMETRY);
    GO

    INSERT INTO SpatialTable (Cell, GeomCol)
    VALUES ('A1', 'POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0))');
    GO

    INSERT INTO SpatialTable (Cell, GeomCol)
    VALUES ('A2', 'POLYGON ((101 0, 200 0, 200 100, 101 100, 101 0))');
    GO

    INSERT INTO SpatialTable (Cell, GeomCol)
    VALUES ('B1', 'POLYGON ((0 101, 100 101, 100 200, 0 200, 0 101))');
    GO

    INSERT INTO SpatialTable (Cell, GeomCol)
    VALUES ('B2', 'POLYGON ((101 101, 200 101, 200 200, 101 200, 101 101))');
    GO

    DECLARE @Point GEOMETRY;

    SET @Point = 'POINT (25 130)'
    SELECT
    Cell
    FROM
    SpatialTable
    WHERE
    GeomCol.STIntersects(@Point) = 1;

    SET @Point = 'POINT (105 40)'
    SELECT
    Cell
    FROM
    SpatialTable
    WHERE
    GeomCol.STIntersects(@Point) = 1;





    The SQL Guy @ blogspot

    @SeanPearceSQL

    About Me
    haggisns
    haggisns
    Grasshopper
    Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

    Group: General Forum Members
    Points: 18 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.
    Sean Pearce
    Sean Pearce
    SSCommitted
    SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

    Group: General Forum Members
    Points: 1770 Visits: 3432
    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

    @SeanPearceSQL

    About Me
    haggisns
    haggisns
    Grasshopper
    Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

    Group: General Forum Members
    Points: 18 Visits: 41
    Yes, they are always squares.
    dwain.c
    dwain.c
    SSCertifiable
    SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

    Group: General Forum Members
    Points: 7261 Visits: 6431
    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_Y
    FROM Points a
    JOIN 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!
    My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
    Sean Pearce
    Sean Pearce
    SSCommitted
    SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

    Group: General Forum Members
    Points: 1770 Visits: 3432
    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 Squares
    ADD 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
    Squares
    WHERE
    (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

    @SeanPearceSQL

    About Me
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search