gravitiq

PerfomanceLab_SQL_LEARNDB

Jul 31st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 5.75 KB | None | 0 0
  1. --------Задание 2------------
  2. -- 1)
  3. SELECT ROUND(AMT)
  4. FROM ORDERS
  5.  
  6. --2)
  7. SELECT SNAME, DECODE(CITY,NULL,'Не задан',CITY)
  8. FROM Sales
  9.  
  10. --3)
  11. SELECT ('Рейтинг покупателя ' || UPPER(cname) || ' равен ' || rating ||
  12. CASE
  13.     WHEN rating > 200 THEN ' (Высокий)'
  14.     WHEN rating = 200 THEN ' (Средний)'
  15.     ELSE ' (Низкий)'
  16. END) AS Smth
  17. FROM Clients
  18.  
  19.  
  20. --------Задание 3------------
  21. --1)
  22. SELECT COUNT(*)
  23. FROM Orders
  24.  
  25. --2)
  26. SELECT AVG(LENGTH(CNAME)) FROM
  27. (SELECT  DISTINCT CNAME
  28. FROM Clients)
  29.  
  30. ))
  31. FROM Clients
  32.  
  33.  
  34. --3)
  35. SELECT ODATE, MAX(AMT)
  36. FROM Orders
  37. GROUP BY ODATE
  38. ORDER BY MAX(AMT) DESC
  39.  
  40. --4)
  41. SELECT CITY, SUM(RATING)
  42. FROM Clients
  43. GROUP BY CITY
  44. HAVING SUM(RATING) > 100
  45.  
  46. --------Задание 4------------
  47. --1)
  48. SELECT DISTINCT s.SNAME, c.CNAME
  49. FROM Sales s
  50. JOIN Clients c
  51. ON s.SNUM = c.SNUM
  52.  
  53. --2)
  54. SELECT *
  55. FROM Sales s
  56. JOIN Clients c
  57. ON s.SNUM = c.SNUM
  58. WHERE c.CITY IN ('London','Moscow','Sochi')
  59.  
  60. --3)
  61. SELECT *
  62. FROM Sales s
  63. JOIN Clients c
  64. ON s.SNUM = c.SNUM
  65. WHERE s.CITY = c.CITY
  66.  
  67. --4)
  68. SELECT s.SNAME, SUM(o.AMT)
  69. FROM Sales s
  70. JOIN Orders o
  71. ON s.SNUM = o.SNUM
  72. GROUP BY s.SNAME
  73. ORDER BY SUM(o.AMT) DESC
  74.  
  75. -- Orders - Заказы
  76. -- Clients - Клиенты
  77. -- Sales - Продавцы
  78.  
  79. -----------5--------------------
  80. --1)
  81. WITH
  82.     s AS (SELECT SNUM
  83.     FROM Sales
  84.     WHERE CITY = 'London')
  85. SELECT *
  86. FROM Orders o
  87. WHERE o.SNUM IN (SELECT * FROM s)
  88.  
  89. --2)
  90. WITH
  91.     s AS (SELECT SNUM
  92.     FROM Sales
  93.     WHERE CITY IN ('London','Springfield'))
  94. SELECT *
  95. FROM Orders o
  96. WHERE o.SNUM IN (SELECT * FROM s)
  97.  
  98. --3.1)
  99. WITH
  100.     cte1 AS (SELECT SNUM, AVG(COMM) AS COMMAVG
  101.     FROM Sales
  102.     GROUP BY SNUM
  103.     ORDER BY SNUM)
  104. SELECT *
  105. FROM Sales s
  106. WHERE COMM > (SELECT COMMAVG FROM cte1 WHERE s.SNUM = cte1.SNUM)
  107.  
  108. --3.2)
  109. WITH
  110.     cte1 AS (SELECT AVG(COMM) AS COMMAVG
  111.     FROM Sales)
  112. SELECT *
  113. FROM Sales s
  114. WHERE COMM > (SELECT COMMAVG FROM cte1)
  115.  
  116. --4)
  117. WITH
  118.     cte1 AS (SELECT CITY, COUNT(*) AS num1 FROM Sales GROUP BY CITY ORDER BY City),
  119.     cte2 AS (SELECT CITY, COUNT(*) AS num2 FROM Clients GROUP BY CITY ORDER BY City),
  120.     cte3 AS (SELECT * FROM cte1 UNION SELECT * FROM cte2)
  121. SELECT CITY, SUM(NUM1)
  122. FROM cte3
  123. GROUP BY CITY
  124. ORDER BY CITY
  125.  
  126. --5)
  127. SELECT Sales.*, CITY
  128. FROM Sales
  129. ORDER BY SNAME
  130.  
  131.  
  132. WITH
  133.     cte1 AS (
  134.               SELECT ROW_NUMBER() OVER (ORDER BY SNAME) AS rownumber, Sales.*
  135.               FROM SALES
  136.             )
  137. SELECT SNUM, SNAME, CITY, COMM
  138. FROM cte1
  139. WHERE rownumber IN (2,3,4)
  140.  
  141.  
  142. -----------6--------------------
  143.  
  144.  
  145. --1)
  146. SELECT SNAME, SNUM
  147. FROM Sales s
  148. WHERE EXISTS (SELECT SNUM
  149.               FROM Clients c
  150.               WHERE s.SNUM = c.SNUM)
  151.  
  152. --2)
  153. SELECT SNAME
  154. FROM Sales s1
  155. WHERE COMM > (SELECT AVG(COMM) AS COMMAVG
  156.                 FROM Sales s2
  157.                 WHERE s1.CITY = s2.CITY)
  158. ORDER BY CITY
  159.  
  160. --3)
  161. SELECT DISTINCT SNAME, SNUM
  162. FROM Sales s
  163. WHERE EXISTS (SELECT *
  164.               FROM Clients c
  165.               WHERE s.CITY = c.CITY
  166.               AND s.SNUM != c.SNUM)
  167. ORDER BY SNAME
  168.  
  169. --4)
  170. SELECT DISTINCT SNAME, s.SNUM
  171. FROM Sales s
  172. JOIN Clients c
  173. ON s.CITY = c.CITY
  174. WHERE s.SNUM != c.SNUM
  175. ORDER BY SNAME
  176.  
  177. --5)
  178. WITH
  179.     etc1 AS (SELECT ODATE, AVG(AMT) AS AVGAMT
  180.              FROM Orders o2
  181.              GROUP BY ODATE)
  182. SELECT o1.ONUM, o1.AMT - o2.AVGAMT AS disp
  183. FROM Orders o1, etc1 o2
  184. WHERE o1.ODATE = o2.ODATE AND o1.ONUM IS NOT NULL
  185. ORDER BY ONUM
  186.  
  187. ---------7--------
  188. --1)
  189. UPDATE Clients
  190. SET CITY = UPPER(CITY)
  191. WHERE CITY LIKE 'S%'
  192.  
  193. COMMIT
  194.  
  195. ROLLBACK
  196.  
  197. SELECT *
  198. FROM Clients
  199. WHERE CITY LIKE 'S%'
  200.  
  201.  
  202. --2)
  203. SELECT *
  204. FROM Sales
  205. ORDER BY SNUM
  206.  
  207. INSERT INTO Sales
  208. VALUES (1, 'Alex', 'Anapa', 1)
  209.  
  210. COMMIT
  211.  
  212. DELETE
  213.  
  214. ROLLBACK
  215.  
  216. DELETE
  217. FROM Sales
  218. WHERE SNAME = 'Alex'
  219.  
  220. --3)
  221. SELECT *
  222. FROM Sales
  223. WHERE CITY IS NULL
  224. AND LENGTH(SNAME) > 8
  225.  
  226. DELETE
  227. FROM Sales
  228. WHERE CITY IS NULL
  229. AND LENGTH(SNAME) > 8
  230.  
  231. --------8------------
  232. --1)
  233. CREATE TABLE myTable AS
  234. SELECT *
  235. FROM Sales
  236. WHERE SNUM IS NOT NULL
  237. AND SNAME IS NOT NULL
  238. AND CITY IS NOT NULL
  239. AND COMM IS NOT NULL
  240.  
  241. DROP TABLE myTable
  242.  
  243. SELECT *
  244. FROM myTable
  245.  
  246. --2)
  247. CREATE VIEW viewMyTable AS
  248. SELECT sname, city
  249. FROM myTable
  250.  
  251.  
  252.  
  253. --3)
  254. DROP TABLE myTable
  255.  
  256. --4)
  257. SELECT *
  258. FROM viewMyTable
  259.  
  260. --5)
  261. --5.1)
  262. CREATE TABLE myTable AS
  263. SELECT *
  264. FROM Sales
  265. WHERE SNUM IS NOT NULL
  266. AND SNAME IS NOT NULL
  267. AND CITY IS NOT NULL
  268. AND COMM IS NOT NULL
  269.  
  270. --5.2)
  271. SELECT *
  272. FROM viewMyTable
  273.  
  274. ------9----------
  275. DESC Clients
  276. --1)
  277. CREATE TABLE C
  278. (CNUM,
  279. CNAME,
  280. CITY,
  281. RATING,
  282. SNUM) AS
  283. SELECT *
  284. FROM Clients c
  285. GROUP BY CNUM, CNAME, CITY, RATING,SNUM
  286. ORDER BY CNUM
  287.  
  288. SELECT DISTINCT *
  289. FROM Clients c
  290.  
  291. SELECT *
  292. FROM C
  293.  
  294. DROP TABLE C
  295.  
  296. --2)
  297. CREATE TABLE S
  298. (
  299. SNUM,
  300. SNAME,
  301. CITY,
  302. COMM
  303. ) AS
  304. SELECT *
  305. FROM Sales
  306. GROUP BY SNUM, SNAME, CITY, COMM
  307. ORDER BY SNUM
  308.  
  309. SELECT *
  310. FROM S
  311.  
  312. --3)
  313. SELECT *
  314. FROM S
  315.  
  316. -- Delete NULLs in snum
  317. DELETE FROM S
  318. WHERE SNUM IS NULL
  319.  
  320. -- Delete duplicates in snum
  321. SELECT DISTINCT SNUM
  322. FROM Sales
  323. ORDER BY SNUM
  324.  
  325. ALTER TABLE S
  326. ADD PRIMARY KEY (SNUM);
  327.  
  328.  
  329.  
  330.  
  331. --1)
  332. CREATE TABLE C
  333. (CNUM,
  334. CNAME,
  335. CITY,
  336. RATING,
  337. SNUM) AS
  338. (SELECT DISTINCT *
  339. FROM Clients c)
  340.  
  341. SELECT *
  342. FROM C
  343.  
  344. --2)
  345. CREATE TABLE S
  346. (
  347. SNUM,
  348. SNAME,
  349. CITY,
  350. COMM
  351. ) AS
  352. (SELECT DISTINCT *
  353. FROM Sales s)
  354.  
  355. SELECT *
  356. FROM Sales s
  357. ORDER BY SNUM
  358.  
  359. SELECT *
  360. FROM S
  361. ORDER BY SNUM
  362.  
  363. DROP TABLE S
  364.  
  365. --3)
  366. -- Delete NULLs in snum
  367. DELETE FROM S
  368. WHERE SNUM IS NULL
  369.  
  370. SELECT *
  371. FROM S
  372. ORDER BY SNUM
  373.  
  374. -- Delete duplicates in snum
  375. DELETE FROM S
  376. WHERE ROWID NOT IN
  377. (SELECT MIN(ROWID)
  378. FROM S
  379. GROUP BY SNUM)
  380.  
  381.  
  382. ALTER TABLE S
  383. ADD PRIMARY KEY (SNUM);
  384.  
  385. --4)
  386.  
  387. SELECT *
  388. FROM C
  389. ORDER BY SNUM
  390.  
  391. DELETE FROM C
  392. WHERE SNUM NOT IN
  393. (SELECT SNUM
  394. FROM S)
  395.  
  396. ALTER TABLE C
  397. ADD FOREIGN KEY (SNUM)
  398. REFERENCES S (SNUM)
Add Comment
Please, Sign In to add comment