Forum Replies Created

Viewing 9 posts - 1 through 10 (of 10 total)

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    Did you try this:

    DECLARE @Boston_prdID (numeric)

    SET @Boston_prdID = 555

    SELECT p.*, c.*, d.DistanceToBoston AS [MinDistanceToBoston]

    FROM PRODUCTS p

    INNER JOIN OTM o ON o.prdID = p.prdID

    INNER JOIN CITIES c ON...

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    The definition fior the cities table doesn't include lambertX and lambertY

    :blush:

    my mistake. Just replace latitude by lambertX and longitude by lambertY in the CITIES definitions.

    Sorry!

    Chris

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    Some sample data would be just grand!

    here you go 🙂

    --creation of the getDistanceFromID scalar function

    CREATE FUNCTION [dbo].[getDistanceFromID]

    (

    @localID numeric, @localX float, @localY float

    )

    RETURNS float

    AS

    BEGIN

    DECLARE @i float

    DECLARE @lX float,...

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    I've used a LEFT JOIN here so you can see the closest city and other cities (rejected rows in the original) in the output.

    Chris,

    Just read the Best practices,...

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    Here's a query which will work.

    [font="Courier New"]DECLARE @Boston_prdID (numeric)

    SET @Boston_prdID = 555

    SELECT p.*, c.*

    FROM PRODUCTS p

    INNER JOIN OTM o ON o.prdID = o.prdID

    INNER JOIN CITIES c...

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    Lovely stuff, thanks Chris. Last question - can you confirm that you're using SQL Server 2000 and not 2005?

    Yes unfortunately.

    I've heard of the OVER PARTITION option that's available...

  • RE: Help needed for a twisted SQL query

    To give you an idea, I tried this subquery to retrieve an individual product and the distance to the nearest location. It works great as is but as you can...

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    Ok lets confirm this:

    Say your reference city is Boston, your requirement would be:

    For each and every individual product (which has been sold), list the nearest city to Boston...

  • RE: Help needed for a twisted SQL query

    Chris Morris (12/5/2008)


    Chris,

    Do you mean this:

    retrieve unique products (prdID) AND the city it's in (postcodeID) AND the shortest distance to a reference city

    Cheers

    ChrisM

    Hi ChrisM,

    That's it exactly !!!

    Chris

Viewing 9 posts - 1 through 10 (of 10 total)