SQL_2 - recursive queries, combined outer joins

select col1, cast(round(avg(col2),2) as decimal(9,2)) as aver_col2
from tab1
group by col1
order by col1


recursive query:
with exp1 (col1, col2, col3) as
(
-------- select top nodes
    select ... from tab1 where ...

union

-------- select other nodes recursively
    select ... from tab1, exp1 where ...

)
select ... from exp1


Here is an example:

with exp1 (row_id, par_postn_id, X_POSITION_NAME, PARENT_NAME)
as (

  select
    pp.row_id, pp.par_postn_id,
    pp.X_POSITION_NAME, '' PARENT_NAME
  from inst1.s_postn pp
  where pp.par_postn_id is null

  union all

  select
    tt.row_id, tt.par_postn_id,
    tt.X_POSITION_NAME, ee.X_POSITION_NAME PARENT_NAME
  from inst1.s_postn tt, exp1 ee
  where ee.row_id = tt.par_postn_id

   )

select
  p.PARENT_NAME, e.Last_Name, e.FST_NAME,
  e.login, e.pager_pin as rep_id, p.X_POSITION_NAME
from
  inst1.s_employee e,
  inst1.s_emp_postn ep,
  exp1 p
where e.row_id = ep.emp_id
  and ep.position_id = p.row_id
order by p.PARENT_NAME, e.Last_Name, e.FST_NAME

combined outer joins:
select empno, deptno, projname
from ( employee left outer join project on respemp=empno )
  left outer join department on mgrno = empno

你可能感兴趣的:(sql)