I have created a table
create table routes (
type character varying
, quality character varying
, route integer
, fare integer
);
insert into routes (type, quality, route, fare)
values
('X', 'GOOD', 1, 5)
,('Y', 'GOOD', 1, 7)
,('X', 'AVERAGE', 2, 10)
,('Y', 'GOOD', 2, 7)
,('X', 'BAD', 3, 8)
,('Y', 'BAD', 3, 15);
For each route number there are 2 records, I need to select one row(route,fare
) for each route based on type
and quality
.
For example if the type
is 'X'
and quality
is 'GOOD'
or 'AVERAGE'
, I will select the route
and fare
of that row and move over to next 'route' . Otherwise, I will select the 'route' and 'fare' from row with type 'Y'. That means from the above table, I should get:
1,5
2,10
3,15
I couldn't figure out to looping through the records of each route and derive the output from the two records of same route.