How to prevent duplicate or similar data from getting processed? I have a stored procedure which calculates the distance between 2 coordinate points:

CREATE PROC loc.CalcDistanceByPostalCode
(
@From VARCHAR(10),
@To   VARCHAR(10)
)
AS
BEGIN
    DECLARE @data table (From_Place VARCHAR(200), From_Region VARCHAR(200), "To_Place" VARCHAR(200), To_Region VARCHAR(200), Distance decimal(8,2))
    DECLARE @postal1 table (ID int IDENTITY(1,1), place_name VARCHAR(200), region_name VARCHAR(200), latitude decimal(12,9), longitude decimal(12,9))
    DECLARE @postal2 table (ID int IDENTITY(1,1), place_name VARCHAR(200), region_name VARCHAR(200), latitude decimal(12,9), longitude decimal(12,9)) 

    INSERT INTO @postal1 SELECT distinct l.place_name, l.region_name, l.latitude, l.longitude 
                         from loc.locations l 
                         cross join loc.locations_postal lp 
                         where postal_code like @From and lp.city_id = l.ID
    INSERT INTO @postal2 SELECT distinct l.place_name, l.region_name, l.latitude, l.longitude 
                         from loc.locations l 
                         cross join loc.locations_postal lp 
                         where postal_code like @To and lp.city_id = l.ID

    INSERT INTO @data 
    SELECT
        t1.place_name,  t1.region_name, t2.place_name, t2.region_name,
        loc.FN_CalcAerialDistance(t1.latitude, t1.longitude, t2.latitude, t2.longitude) -- Function calculates Distance
    FROM @postal1 as t1
    CROSS JOIN @postal2 as t2
    WHERE t1.latitude &lt;&gt; t2.latitude and t1.longitude &lt;&gt; t2.longitude

    DECLARE @Distance decimal(8,2) = 0
    select * from @data order by Distance desc
    SELECT @Distance = Distance from @data
    return @Distance;
END


It calculates correct result if @From and @To are different, but gives redundant data if @From and @To are same.

Example: Different @From and @To:

EXEC loc.CalcDistanceByPostalCode @From = '110%', @To = '400%'`


Result:

From_Place  From_Region To_Place     To_Region      Distance
Delhi       Delhi        Mumbai      Maharashtra    1160.65
New Delhi   Delhi        Mumbai      Maharashtra    1157.80
Delhi       Delhi        Vasai-Virar Maharashtra    1115.68
New Delhi   Delhi        Vasai-Virar Maharashtra    1112.87


Example: Same @From and @To:

EXEC loc.CalcDistanceByPostalCode @From = '110%', @To = '110%'`


Result:

From_Place  From_Region To_Place    To_Region   Distance
New Delhi   Delhi       Delhi       Delhi       3.51
Delhi       Delhi       New Delhi   Delhi       3.51


Do I need to write extra code to check if Distance is same then compare the From_Place with To_Place with the rows having the same distance? or any optimised way to prevent the duplicate data?
Try adding And t1.ID &gt; t2.ID to the where clause , it will make sure that the join happens once .
To the following query

SELECT t1.place_name, t1.region_name, t2.place_name, t2.region_name, loc.FN_CalcAerialDistance(t1.latitude, t1.longitude, t2.latitude, t2.longitude) -- Function calculates Distance 
FROM @postal1 as t1 CROSS JOIN @postal2 as t2 
WHERE t1.latitude &lt;&gt; t2.latitude and t1.longitude &lt;&gt; t2.longitude