SQL select in 1 line

81 views Asked by At

I have a table with the below data

route abnum knanf knend
00012 1 PI SY
00012 2 SY PA
00012 3 PA HN

and the query

select knanf as start, knend as end from TVRAB as a
where route = '00012' and
  ( abnum = ( select min( ABNUM ) from TVRAB where route = a~route ) or
    abnum = ( select max( ABNUM ) from TVRAB where route = a~route ) )

The results are 2 lines

START END
PI SY
PA HN

Can we have in 1 line the MIN KNANF and the MAX KNEND e.g.

START END
PI HN

Thanks

1

There are 1 answers

0
Satish Kumar On

Below query produces the output you are looking for.

  SELECT a1~knanf AS start, a2~knend AS end FROM tvrab AS a1
   INNER JOIN tvrab AS a2
  ON a1~route = a2~route
  WHERE a1~route = '00012'
  AND a1~abnum = ( SELECT MIN( abnum ) FROM tvrab WHERE route = a1~route )
  AND a2~abnum = ( SELECT MAX( abnum ) FROM tvrab WHERE route = a1~route )