select EmpID, FirstName, LastName, ManagerID, coalesce(Managerid,-1)
from Employee
where ManagerID is NULL
union all
select Employee.EmpID, Employee.FirstName, Employee.LastName, Employee.ManagerID, coalesce(Employee.ManagerID, 1)
from Employee
where Employee.ManagerID is NOT NULL
order by 5 asc;
empid | firstname | lastname | managerid | coalesce
-------+-----------+----------+-----------+----------
201 | Bob | Smith | | -1
101 | John | Doe | | -1
103 | Jonny | Doe | 101 | 101
102 | Jo | Doe | 101 | 101
104 | Jennie | Doe | 101 | 101
202 | Betty | Smith | 201 | 201
204 | Brad | Smith | 201 | 201
203 | Brent | Smith | 201 | 201
(8 rows)
--
You received this message because you are subscribed to the Google Groups "Greenplum Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gpdb-users+...@greenplum.org.
To post to this group, send email to gpdb-...@greenplum.org.
Visit this group at https://groups.google.com/a/greenplum.org/group/gpdb-users/.
For more options, visit https://groups.google.com/a/greenplum.org/d/optout.
--1) Write UDF to recursively explode a table
create or replace function explode_table(input_table text)
returns text
as
$$
import plpy
sql = """
drop table if exists exploded_table;
create table exploded_table
(
part_id int,
sub_part_id int,
subpart_attribute text
) distributed randomly;
"""
plpy.execute(sql)
explosion_complete = False
while(not explosion_complete):
sql = """
drop temp table if exists exploded_temp;
create temp table exploded_temp
as
(
select
t1.sub_part_id as part_id,
t2.part_id as sub_part_id,
t2.subpart_attribute
from
{input_table} t1,
{input_table} t2
where
t1.sub_part_id = t2.part_id
) distributed randomly;
""".format(
input_table=input_table
)
plpy.execute(sql)
num_rows_sql= """
select
count(*) as num_rows
from
exploded_temp
"""
num_rows = plpy.execute(num_rows_sql)[0]['num_rows']
if num_rows == 0:
explosion_complete = True
else:
sql = """
insert into exploded_table
select
*
from
exploded_temp;
"""
plpy.execute(sql)
return 'Successfully exploded {0}'.format(input_table)
$$language plpythonu;
--2) Invoke the UDF to explode the table
select
explode_table('parts_subparts_relation_table');
--
You received this message because you are subscribed to the Google Groups "Greenplum Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gpdb-users+...@greenplum.org.
To post to this group, send email to gpdb-...@greenplum.org.
Visit this group at https://groups.google.com/a/greenplum.org/group/gpdb-users/.
For more options, visit https://groups.google.com/a/greenplum.org/d/optout.