Max_Leb

Untitled

Jan 9th, 2023
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.33 KB | None | 0 0
  1. -- TASK 6
  2.  
  3. SELECT Сategories.name, Products.name, Products.in_stock, SUM(Products.price * Products.in_stock) -- (1)
  4. OVER(PARTITION BY Сategories.name ORDER BY Products.name) AS Total_price FROM Products
  5. JOIN Сategories ON Сategories.id = Products.category_id;
  6.  
  7.  
  8. SELECT Products.name, Products.in_stock, AVG(Products.price)                                      -- (2)
  9. OVER(ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS Average_price FROM Products;
  10.  
  11.  
  12. WITH Items(name, price, manufacturer, supplier) AS                                                -- (3)
  13. (SELECT Products.name, Products.price, Manufacturers.name, Suppliers.name FROM Products
  14. JOIN Manufacturers, Suppliers ON Manufacturers.id = Products.manufacturer_id AND Suppliers.id = Products.supplier_id)
  15. SELECT name, manufacturer, supplier, price FROM Items WHERE price > 200;
  16.  
  17.  
  18.  
  19. WITH Providers_and_Producers(manufacturer_name, product_name, product_count, product_price) AS   -- (4)
  20.   (
  21.     SELECT Suppliers.name, Products.name, Products.price, Products.in_stock FROM Products
  22.     JOIN Suppliers ON Suppliers.id = Products.supplier_id
  23.     INTERSECT
  24.     SELECT Manufacturers.name, Products.name, Products.price, Products.in_stock FROM Products
  25.     JOIN Manufacturers ON Manufacturers.id = Products.manufacturer_id
  26.   )
  27. SELECT * FROM Providers_and_Producers;
  28.  
  29.  
  30.  
  31. CREATE TABLE IF NOT EXISTS Companies(                                                           -- (5)
  32.     id INTEGER PRIMARY KEY AUTOINCREMENT,
  33.     subdivision_name TEXT NOT NULL,
  34.     parent_id INTEGER
  35. );
  36. INSERT INTO Companies(subdivision_name, parent_id) VALUES
  37. ('Department_IT', NULL),
  38. ('IT_managment', 1),
  39. ('Development_and_control_department', 1),
  40. ('Information_systems_department', 1),
  41. ('Server_hardware_department', 2),
  42. ('User_support_department', 2),
  43. ('Communications_department', 2),
  44. ('Quality_control_department', 3),
  45. ('Project_office', 3);
  46.  
  47. WITH RECURSIVE IT_hierarchy AS (
  48.   SELECT Companies.id, Companies.parent_id, Companies.subdivision_name, 2 AS level
  49.   FROM Companies WHERE Companies.subdivision_name = 'IT_managment'
  50.   UNION ALL
  51.   SELECT Companies.id, Companies.parent_id, Companies.subdivision_name, IT_hierarchy.level + 1 AS level FROM IT_hierarchy, Companies
  52.   WHERE Companies.parent_id = IT_hierarchy.id
  53.   )
  54. SELECT subdivision_name FROM IT_hierarchy WHERE subdivision_name != 'IT_managment';
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment