Advertisement
gravitiq

HomeWork

Jul 31st, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 6.24 KB | None | 0 0
  1. --1)
  2. SELECT CUST_FIRST_NAME, CUST_LAST_NAME
  3. FROM CUSTOMERS
  4. WHERE CUST_GENDER = 'F'
  5. AND CUST_MARITAL_STATUS = 'married'
  6. AND COUNTRY_ID NOT IN ('JP', 'BR', 'IT')
  7. AND CUST_CREDIT_LIMIT = (SELECT MIN(CUST_CREDIT_LIMIT) FROM CUSTOMERS)
  8.  
  9.  
  10. --2)
  11. SELECT
  12. 'NAME: '||
  13. cust_first_name ||
  14. '; city: ' ||
  15. cust_city ||
  16. '; address: '||
  17. cust_street_address||
  18. '; number:'||
  19. cust_main_phone_number||
  20. '; email: '||
  21. cust_email||
  22. ';' AS OneColumn
  23. FROM (SELECT *
  24.         FROM CUSTOMERS
  25.         WHERE CUST_MAIN_PHONE_NUMBER LIKE '%77') t
  26. WHERE LENGTH(CUST_STREET_ADDRESS) = (SELECT MAX(LENGTH(t.CUST_STREET_ADDRESS))
  27.         FROM (SELECT *
  28.         FROM CUSTOMERS
  29.         WHERE CUST_MAIN_PHONE_NUMBER LIKE '%77') t)
  30.        
  31. --3)
  32.  
  33. WITH
  34. etc0 AS (
  35.     SELECT * -- Customers старше 1980
  36.     FROM CUSTOMERS
  37.     WHERE CUST_YEAR_OF_BIRTH > 1980),
  38. etc1 AS ( -- Минимальная цена продукта для категорий
  39.     SELECT MIN(PROD_LIST_PRICE) AS minprc
  40.     FROM Products p
  41.     WHERE PROD_SUBCATEGORY = 'Shirts - Boys'
  42.     OR PROD_SUBCATEGORY = 'Shirts - Girls'),
  43. etc2 AS ( -- Все продукты с этой ценой
  44.     SELECT *
  45.     FROM Products p
  46.     WHERE (PROD_SUBCATEGORY = 'Shirts - Boys'
  47.     OR PROD_SUBCATEGORY = 'Shirts - Girls')
  48.     AND PROD_LIST_PRICE = (SELECT minprc FROM etc1)
  49.     ORDER BY PROD_LIST_PRICE),
  50. etc3 AS ( -- Добавляем к etc2 поле с CUST_ID
  51.     SELECT CUST_ID, etc2.*
  52.     FROM SALES s
  53.     JOIN etc2
  54.     ON etc2.PROD_ID=s.PROD_ID)
  55. SELECT *
  56. FROM etc0 -- Customets старше 1980
  57. WHERE CUST_ID IN (
  58. SELECT CUST_ID
  59. FROM etc3)
  60.    
  61. --4)
  62. SELECT *
  63. FROM CUSTOMERS c1
  64. WHERE EXISTS
  65. (
  66. SELECT *
  67. FROM CUSTOMERS c2
  68. WHERE c1.CUST_ID=c2.CUST_ID
  69. AND CUST_INCOME_LEVEL LIKE 'D:%'
  70. AND CUST_GENDER = 'M'
  71. AND CUST_MARITAL_STATUS IS NULL
  72. AND (COUNTRY_ID IN ('US','DE')))
  73.    
  74. --5)
  75. WITH
  76. etc1 AS (
  77.     SELECT PROD_ID, PROD_LIST_PRICE
  78.     FROM Products p),
  79. etc2 AS(
  80.     SELECT PROD_ID, CUST_ID
  81.     FROM Sales s),
  82. etc3 AS (
  83.     SELECT CUST_ID, COUNTRY_ID
  84.     FROM Customers),
  85. etc4 AS (
  86.     SELECT COUNTRY_ID, COUNTRY_NAME
  87.     FROM Countries),
  88. etc5 AS(
  89.     SELECT CUST_ID, PROD_LIST_PRICE
  90.     FROM etc1
  91.     JOIN etc2
  92.     ON etc1.PROD_ID = etc2.PROD_ID),
  93. etc6 AS(
  94.     SELECT COUNTRY_ID, PROD_LIST_PRICE
  95.     FROM etc5    
  96.     JOIN etc3
  97.     ON etc5.CUST_ID = etc3.CUST_ID),
  98. etc7 AS(
  99.     SELECT COUNTRY_NAME, PROD_LIST_PRICE
  100.     FROM etc4
  101.     JOIN etc6
  102.     ON etc4.COUNTRY_ID = etc6.COUNTRY_ID)
  103. SELECT COUNTRY_NAME, AVG(PROD_LIST_PRICE)
  104. FROM etc7
  105. GROUP BY COUNTRY_NAME
  106. ORDER BY AVG(PROD_LIST_PRICE) DESC
  107.  
  108. --6)
  109. SELECT REGEXP_SUBSTR(CUST_EMAIL,'\w*.\w*$'), COUNT(*)
  110. FROM Customers c
  111. GROUP BY REGEXP_SUBSTR(CUST_EMAIL,'\w*.\w*$')
  112.  
  113. --7)
  114. WITH
  115.     etc1 AS ( -- PROD_ID для категории "Мужчины"
  116.         SELECT PROD_ID
  117.         FROM Products
  118.         WHERE PROD_CATEGORY = 'Men'
  119.         ),
  120.     etc2 AS (
  121.         SELECT CUST_ID, QUANTITY_SOLD
  122.         FROM Sales
  123.         WHERE PROD_ID IN (SELECT PROD_ID FROM etc1)
  124.         ),
  125.     etc3 AS (
  126.         SELECT CUST_ID, COUNTRY_ID
  127.         FROM Customers c
  128.         ),
  129.     etc4 AS (        
  130.         SELECT COUNTRY_ID, QUANTITY_SOLD
  131.         FROM etc2
  132.         JOIN etc3
  133.         ON etc2.CUST_ID = etc3.CUST_ID    
  134.         ),
  135.     etc5 AS (
  136.         SELECT COUNTRY_ID, COUNTRY_NAME
  137.         FROM Countries
  138.         ),
  139.     etc6 AS (    
  140.         SELECT COUNTRY_NAME, SUM(QUANTITY_SOLD) AS SUMQS
  141.         FROM etc4
  142.         JOIN etc5
  143.         ON etc4.COUNTRY_ID = etc5.COUNTRY_ID
  144.         GROUP BY COUNTRY_NAME        
  145.         )
  146. SELECT *
  147. FROM etc6
  148. WHERE SUMQS > (SELECT AVG(SUMQS) FROM etc6)  
  149.  
  150. --8)
  151. WITH
  152.     etc1 AS (
  153.         SELECT COUNTRY_ID, COUNT(*) AS M_QUANTITY
  154.         FROM Customers
  155.         WHERE CUST_GENDER = 'M'
  156.         GROUP BY COUNTRY_ID
  157.         ),
  158.     etc2 AS (
  159.         SELECT COUNTRY_ID, COUNT(*) AS F_QUANTITY
  160.         FROM Customers
  161.         WHERE CUST_GENDER = 'F'
  162.         GROUP BY COUNTRY_ID
  163.         ),
  164.     etc3 AS (        
  165.         SELECT etc1.COUNTRY_ID AS СТРАНА,
  166.         ROUND(M_QUANTITY/(M_QUANTITY+F_QUANTITY)*100,1) AS  Процент_мужчин,
  167.         ROUND(F_QUANTITY/(M_QUANTITY+F_QUANTITY)*100,1) AS  Процент_женщин        
  168.         FROM etc1
  169.         JOIN etc2
  170.         ON etc1.COUNTRY_ID = etc2.COUNTRY_ID
  171.         ),
  172.     etc4 AS (
  173.         SELECT COUNTRY_ID, COUNTRY_NAME
  174.         FROM COUNTRIES
  175.         )
  176. SELECT etc4.COUNTRY_NAME AS СТРАНА, Процент_мужчин AS "% мужчин", Процент_женщин  AS "% женщин"
  177. FROM etc3
  178. JOIN etc4
  179. ON etc3.СТРАНА = etc4.COUNTRY_ID
  180. ORDER BY etc4.COUNTRY_NAME
  181.  
  182. --9)
  183. WITH
  184.     etc1 AS (
  185.         SELECT PROD_ID, TIME_ID,MAX(QUANTITY_SOLD) AS MAXSQ
  186.         FROM SALES s2
  187.         GROUP BY TIME_ID, PROD_ID
  188.         ORDER BY PROD_ID
  189.         ),
  190.     etc2 AS (
  191.         SELECT MAX(MAXSQ) AS MAXSQ2, PROD_NAME  
  192.         FROM etc1
  193.         JOIN PRODUCTS p
  194.         ON etc1.PROD_ID = p.PROD_ID
  195.         GROUP BY PROD_NAME
  196.         ORDER BY MAXSQ2 DESC
  197.         )
  198. SELECT MAXSQ2 AS "Макс покуп/день", PROD_NAME
  199. FROM etc2
  200. WHERE ROWNUM <=20
  201.  
  202. --10)
  203. WITH
  204.     etc1 AS (
  205.         SELECT TIME_ID, PROD_SUBCATEGORY, MAX(QUANTITY_SOLD) AS MAXSQ
  206.         FROM PRODUCTS p
  207.         JOIN SALES s
  208.         ON p.PROD_ID = s.PROD_ID
  209.         GROUP BY TIME_ID, PROD_SUBCATEGORY
  210.         ORDER BY PROD_SUBCATEGORY
  211.         )
  212. SELECT PROD_SUBCATEGORY, MAX(MAXSQ) AS MAXSQ
  213. FROM etc1
  214. GROUP BY  PROD_SUBCATEGORY  
  215. ORDER BY  MAXSQ DESC
  216.  
  217. --11)
  218. CREATE TABLE "sales_User2_Rostislav_Teryaev" AS
  219. WITH
  220.     etc1 AS (
  221.         SELECT DISTINCT TIME_ID, SUM(QUANTITY_SOLD) AS smqs,REGEXP_SUBSTR(TIME_ID,'\d{2}.\w*$') AS mnth
  222.         FROM sales s
  223.         GROUP BY REGEXP_SUBSTR(TIME_ID,'\d{2}.\w*$'), TIME_ID
  224.         ),
  225.     etc2 AS (  
  226.         SELECT MAX(smqs) AS maxsmqs
  227.         FROM etc1
  228.         ),
  229.     etc3 AS (
  230.         SELECT mnth
  231.         FROM etc1
  232.         WHERE etc1.smqs IN (SELECT maxsmqs FROM etc2)
  233.         ),
  234.     etc4 AS (
  235.         SELECT TIME_ID
  236.         FROM etc1
  237.         WHERE etc1.mnth IN (SELECT mnth FROM etc3)        
  238.         )
  239. SELECT *
  240. FROM Sales s
  241. WHERE s.TIME_ID IN (SELECT TIME_ID FROM etc4)        
  242.  
  243.  
  244. SELECT *
  245. FROM "sales_User2_Rostislav_Teryaev"
  246.  
  247. --14)
  248. DROP TABLE "sales_User2_Rostislav_Teryaev"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement