• mickyT (12/12/2012)


    Hi Dwain

    After reading your article (nice work), I couldn't help myself and added a Geography column to the airports table.

    This allows the distance to be calculated using the Geography method STDistance and you can also start visualising the data

    ALTER TABLE Airports ADD Location Geography

    GO

    UPDATE Airports

    SET Location = Geography::STGeomFromText(

    'POINT (' +

    CAST(CASE WHEN LongDir = 'E' THEN CAST(LongDeg AS DECIMAL(11,8)) ELSE CAST(-LongDeg AS DECIMAL(11,8)) END +

    CAST((LongMin / 60.00) + ( LongSec / 60.00 / 60.00) AS DECIMAL (11,8)) AS varchar(15))

    + ' ' +

    CAST(CASE WHEN LatDir = 'N' THEN CAST(LatDeg AS DECIMAL(11,8)) ELSE CAST(-LatDeg AS DECIMAL(11,8)) END +

    CAST((LatMin / 60.00) + ( LatSec / 60.00 / 60.00) AS DECIMAL (11,8)) AS varchar(15))

    + ')'

    ,4326

    )

    WHERE LatDeg+LatMin+LatSec+LongDeg+LongMin+LongSec <> 0

    SELECT f.flight, o.IATA_Code, d.IATA_Code,

    f.SegmentDistanceMiles, f.SegmentDistanceKM, cast(o.Location.STDistance(d.Location) / 1000 as decimal(9,4)) CalcDistanceKM,

    case when o.location is not null and d.location is not null then

    Geography::STGeomFromText(

    'LINESTRING (' + REPLACE(REPLACE(REPLACE(o.Location.ToString(),'POINT',''),'(',''),')','') +

    ', ' +

    REPLACE(REPLACE(REPLACE(d.Location.ToString(),'POINT',''),'(',''),')','') +

    ')'

    ,

    4326)

    else

    null

    end PathGeom

    FROM dailyflightschedules f

    inner join airports o on f.origin = o.IATA_Code

    inner join airports d on f.destination = d.IATA_Code

    from airports

    Pretty cool Micky!

    Do you need to run anything special on SQL 2008 to support the internal functions you're calling?

    Happy that someone actually read the article.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St