Advertisement
DrSartoriuss

Untitled

Oct 9th, 2024
167
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | Software | 0 0
  1. ### fill the gaps below and send the script back
  2.  
  3. import pandas as pd
  4. import sqlite3
  5.  
  6. DB_NAME = 'inventive_retail_group.db'
  7.  
  8. def create_sqlite_database(filename: str) -> None:
  9.     """ create a database connection to an SQLite database """
  10.     conn = None
  11.     try:
  12.         conn = sqlite3.connect(filename)
  13.         print(sqlite3.sqlite_version)
  14.     except sqlite3.Error as e:
  15.         print(e)
  16.     finally:
  17.         if conn:
  18.             conn.close()
  19.  
  20. create_sqlite_database(DB_NAME)
  21. conn = sqlite3.connect(DB_NAME)
  22.  
  23. df = pd.read_csv('https://gist.githubusercontent.com/kevin336/acbb2271e66c10a5b73aacf82ca82784/raw/e38afe62e088394d61ed30884dd50a6826eee0a8/employees.csv')
  24.  
  25. employees = df.drop('SALARY', axis=1)
  26. salaries = df[['EMPLOYEE_ID', 'SALARY']]
  27.  
  28. employees.to_sql('employees', conn, index=False, if_exists='replace')
  29. salaries.to_sql('salaries', conn, index=False, if_exists='replace')
  30.  
  31. display(employees)
  32. display(salaries)
  33.  
  34. ### find employees which managers work in other department
  35. employees_working_in_other = pd.read_sql('''
  36. SELECT *
  37. FROM employees
  38. WHERE MANAGER_ID IN (SELECT MANAGER_ID
  39.                     FROM employees
  40.                     GROUP BY MANAGER_ID
  41.                     HAVING AVG(DEPARTMENT_ID) NOT IN (20,30,50,60,90,100,110)
  42.                     )                  
  43. ''', conn)
  44. print(employees_working_in_other)
  45.  
  46. ### Calculate average salary for departments using window function
  47. employees_working_in_other = pd.read_sql('''
  48. SELECT DEPARTMENT_ID,
  49.       AVG (SALARY) OVER (PARTITION BY DEPARTMENT_ID) AS avg_salaries
  50. FROM (SELECT *
  51.      FROM employees i
  52.      JOIN salaries AS c ON i.EMPLOYEE_ID = c.EMPLOYEE_ID) AS d
  53.      
  54. ''', conn)
  55. print(employees_working_in_other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement