I am not sure but I think he is looking something like this ; and I am still scratching my head to do this in a proper way..
CREATE TABLE #Trips(
Origin VARCHAR(50)
,Destination VARCHAR(50)
,Tonnage FLOAT
);
INSERT INTO #Trips(Origin, Destination, Tonnage)
VALUES
('Alabama', 'Arizona', 0.0012),
('Alabama', 'Idaho', 1.2),
('Idaho', 'Alaska', 644),
('Arizona', 'Alaska', 665),
('Arizona', 'Alabama', 788),
('Alabama', 'Texas', 12),
('Idaho', 'Alabama', 666),
('Alaska', 'Idaho', 456),
('Alaska', 'Idaho', 0),
('Arizona', 'Michigan', 0)
CREATE TABLE #Trips1(
Origin VARCHAR(50)
,Destination VARCHAR(50)
,Tonnage FLOAT
);
declare @originA varchar(50),@DestinationA varchar(50),@originB varchar(50),@destinationB varchar(50),
@tonnageA float,@tonnageB float
declare trip_cursor cursor for
SELECT distinct a.origin as originA ,
a.destination As destinationA,
a.tonnage As tonnageA,
b.origin as originB,
b.destination as destinationB,
b.tonnage as tonnageB FROM #Trips a
outer apply #trips b
where a.Origin=b.Destination
and a.Destination=b.Origin
and a.tonnage<>0
and b.tonnage<>0
open trip_cursor
fetch next from trip_cursor into
@originA ,@DestinationA ,@tonnageA,@originB ,@destinationB
,@tonnageB
while @@FETCH_STATUS=0
begin
insert into #Trips1 values (@originA,@DestinationA,@tonnageA)
insert into #Trips1 values (@originB,@DestinationB,@tonnageB)
fetch next from trip_cursor into
@originA ,@DestinationA ,@tonnageA,@originB ,@destinationB
,@tonnageB
end
close trip_cursor
deallocate trip_cursor
insert into #Trips1 (origin,destination,tonnage) select origin,destination,tonnage from #Trips where Tonnage<>0
except select origin,destination,tonnage from #Trips1-- where tonnage <>0
select * from #Trips1
drop table #Trips1
still looking for a good solution; it's weekend 😛
~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one :ermm: