select * from larearrelation where rearedgens = 1 and rearflag = 1 and rearlevel = ‘01’ connect by prior agentcode = rearagentcode start with rearagentcode = ‘10032226’; with recursive rs as ( select * from larearrelation where rearagentcode = ‘10032226’ union all select a. *from larearrelation a, rs where a.rearagentcode = rs.agentcode ) select ** from rs where rearedgens = 1 and rearflag = ‘1’ and rearlevel = ‘01’

select max(level) from larearrelation where rearedgens = 1 and rearflag = 1 and rearlevel = ‘01’

connect by prior agentcode = rearagentcode start with rearagentcode = ‘10032226’;

with recursive rs as (

select larearrelation.*, 1 depth from larearrelation where rearagentcode = ‘10032226’

union all

select a./**, rs.depth + 1 depth from larearrelation a, rs where a.rearagentcode = rs.agentcode

select max(rs.depth) from rs where rearedgens = 1 and rearflag = ‘1’ and rearlevel = ‘01’

--创建函数来解决
CREATE FUNCTION add_months(date, int)

RETURNS date AS

'SELECT ($1 + ( $2::text || ''months'')::interval)::date;'

LANGUAGE 'sql'

--创建函数来解决
CREATE FUNCTION LAST_DAY(DATE)

RETURNS DATE AS

'SELECT date(substr(text($1 +  interval(''1 month'')),1,7)||''-01'')-1'

LANGUAGE 'sql';

--创建函数来解决
CREATE FUNCTION LAST_DAY(DATE)

RETURNS DATE AS

'SELECT date(substr(text($1 +  interval(''1 month'')),1,7)||''-01'')-1'

LANGUAGE 'sql';