employees ee)
group by l.city order by AVG_SALARY) union all
(select 'Workers' EMP
,l.city
,count(distinct e.employee_id) CNT_WORKER
,round(avg(e.salary)) AVG_SALARY
from employees e
join locations l using(location_id)
where e.employee_id not in
(select distinct d.manager_id id from departments d
where d.manager_id is not null
union
select distinct e.manager_id id from employees e where
e.manager_id |
is not null) |
|
group by |
l.city |
|
order by |
AVG_SALARY)) q |
|
order by city, AVG_SALARY; |
|
|
''', connection) |
|
|
print(zapros) |
|
|
cursor.callproc('select_data',[30,]) |
# вызов функции |
|
(название из PostgreSQL) |
|
|
result = cursor.fetchall() |
# получение результатов |
|
result_proc = pd.DataFrame(result) |
# создание датафрейма с |
|
результатом |
|
|
print('\n\nРезультат вызова функции select_data:\n', result_proc)
# создаине функции select_data1 cursor.execute("""
CREATE OR REPLACE FUNCTION select_data1(id_dept int) RETURNS SETOF departments AS $$
SELECT * FROM departments WHERE departments.department_id >
21
id_dept;
$$ LANGUAGE SQL;
""") # выполнение запроса connection.commit() #внесение изменений в БД
cursor.callproc('select_data1',[30,]) |
|
# вызов функции |
(название из PostgreSQL) |
|
|
result = cursor.fetchall() |
# |
получение результатов |
result_proc = pd.DataFrame(result) |
# |
создание датафрейма с |
результатом |
|
|
print('\n\nРезультат вызова функции select_data1:\n', result_proc)
# моя функция cursor.execute("""
CREATE OR REPLACE FUNCTION my_func(min_salaryy int) RETURNS table (department_name character
,WORKER_COUNT integer
,AVG_SALARY integer) AS $$
select d.department_name
,count(*) WORKER_COUNT
,round(avg(e.salary)) AVG_SALARY from departments d
join employees e using(department_id) where
e.salary > min_salaryy group by d.department_name; $$ LANGUAGE SQL;
""") # выполнение запроса connection.commit() #внесение изменений в БД
cursor.callproc('my_func',[5000,]) |
# вызов функции (название |
|
из PostgreSQL) |
|
|
result = cursor.fetchall() |
# |
получение результатов |
result_proc = pd.DataFrame(result) |
# |
создание датафрейма с |
22
результатом
print('\n\nРезультат вызова функции my_func:\n', result_proc)
connection.close() # закрытие соединения cursor.close() # закрытие cursor
23