Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 8.30 KB | None | 0 0
  1. drop database task1;
  2. create database task1 character set utf8 /*collate utf8_unicode_ci*/;
  3. use task1;
  4.  
  5.  
  6. create table Users (
  7. id int auto_increment primary key,
  8. Name varchar(20),
  9. login varchar(20),
  10. email varchar(20),
  11. department ENUM('Производство', 'Поддержка пользователей', 'Управление', 'Администрация'));
  12. describe Users;
  13.  
  14.  
  15. create table Project (
  16. id int auto_increment primary key,
  17. Name varchar(15),
  18. Description text not null,
  19. Date_open date not null,
  20. Date_close date);
  21. describe Project;
  22.  
  23.  
  24. create table Tasks (
  25. id int auto_increment primary key,
  26. Project_id int,
  27. Title varchar(15),
  28. Priority int,
  29. Description text,
  30. task_Condition ENUM('Новая','Переоткрыта','Выполняется','Закрыта'),     /*Condition reserved*/
  31. Evaluation int,
  32. Elapsed_time int,
  33. exec_user_id int,
  34. creat_user_id int not null,
  35. creat_date date);
  36. describe Tasks;
  37.  
  38. /*1-2:*/
  39.  
  40. insert into Project (Name, Date_open) values ('РТК', '2016-01-31');         
  41. insert into Project (Name, Date_open, Date_close) values ('СС.Коннект', '2016-01-31', '2016-12-31');
  42. insert into Project (Name, Date_open, Date_close) values ('Демо-Сибирь', '2015-02-23', '2016-01-31');
  43. insert into Project (Name, Date_open, Date_close) values ('МВД-Онлайн', '2016-05-22','2016-01-31');
  44. insert into Project (Name, Date_open) values ('Поддержка', '2016-06-07');
  45.  
  46. insert into Users (name, login, email, department) values ('Касаткин Артем', 'kasatkin', 'kasatka@gmail.com', 'Администрация');
  47. insert into Users (name, login, email, department) values ('Петрова София', 'petrova', 'petrova@gmail.com', 'Бухгалтерия');
  48. insert into Users (name, login, email, department) values ('Дроздов Федр', 'drosdov', 'drosdov@gmail.com', 'Поддержка пользователей');
  49. insert into Users (name, login, email, department) values ('Иванова Василина', 'ivanova', 'ivanova@gmail.com', 'Бухгалтерия');
  50. insert into Users (name, login, email, department) values ('Беркут Алексей', 'aerkut', 'berkut@gmail.com', 'Управление');
  51. insert into Users (name, login, email, department) values ('Белова Вера', 'velova', 'belova@gmail.com', 'Управление');
  52. insert into Users (name, login, email, department) values ('Макенрой Алексей', 'makenroy', 'makenroy@gmail.com', 'Управление');
  53.  
  54. /*insert Tasks*//*TODO*/exec_user_id
  55.  
  56. insert into Tasks (Title, Priority, exec_user_id,creat_user_id) values ('задание', 62, 1,7);
  57. insert into Tasks (Title, Priority, exec_user_id,creat_user_id) values ('еще задание ', 64, 2,7);
  58. insert into Tasks (Title, Priority, exec_user_id) values ('задание2', 73, 3);
  59. insert into Tasks (Title, Priority, exec_user_id) values ('задание3', 1, 1);
  60. insert into Tasks (Title, Priority, exec_user_id,creat_user_id) values ('aas', 32, 4,7);
  61. insert into Tasks (Title, Priority, exec_user_id) values ('xzc', 7, 4);
  62.  
  63.  
  64. insert into Tasks (Title, Priority, exec_user_id, creat_date) values ('xzasdc', 7, 1,'2016-01-01');
  65. insert into Tasks (Title, Priority, exec_user_id, creat_date) values ('asdgas', 7, 1,'2016-01-02');
  66. insert into Tasks (Title, Priority, exec_user_id, creat_date) values ('asdfee', 7, 1,'2016-01-03');
  67.  
  68. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('daf', 1, 2,1);
  69. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('aas', 32, 2,2);
  70. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('xzc', 7, 2,2);
  71. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('qw23e', 62, 1,7);
  72. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('q12we', 3, 2,7);
  73. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('qw12323e', 62, 1,7);
  74. insert into Tasks (Title, Priority, exec_user_id, creat_user_id) values ('qw12323e', 62, 1,7);
  75.  
  76.  
  77. /*1-3:*/
  78.  
  79. /*a*/select * from Tasks;
  80.  
  81. /*b*/select Name, department from Users;
  82.  
  83. /*c*/select login, email from Users;
  84.  
  85. /*d*/select Title from Tasks where Priority>50;
  86.  
  87. /*e*/select Name from Users INNER JOIN Tasks ON Tasks.exec_user_id = Users.id;
  88.  
  89. /*f*/(select distinct exec_user_id from Tasks where exec_user_id not in
  90. (select distinct creat_user_id from Tasks)) union (select creat_user_id from Tasks);
  91.  
  92. /*k*/select * from Tasks where  creat_user_id not in
  93. (select id from Users where Name = 'Петрова София') and exec_user_id in
  94. (select id from Users where Name in ('Иванова Василина','Сидорова','Беркут Алексей'));
  95.  
  96. /*1-4:*/
  97.  
  98. select * from Tasks where exec_user_id = (select id from Users where name = 'Касаткин Артем') and creat_date in ('2016-01-01','2016-01-02','2016-01-03'); /* in or = ?*/
  99.  
  100. /*1-5:*/
  101.  
  102. select * from Tasks where exec_user_id = (select id from Users where name = 'Петрова София') and creat_user_id in (select id from Users where department in ('Администрация', 'Бухгалтерия', 'Развитие'));
  103.  
  104. /*1-6:*/
  105.  
  106. /*b:creat_user_id int not null */
  107.  
  108. insert into Tasks (creat_user_id) values ('1'); /
  109.  
  110. select * from Tasks where exec_user_id is null;
  111.  
  112. update Tasks set exec_user_id = 2 where exec_user_id is null;
  113.  
  114. /*1-7:*/
  115.  
  116. create table Tasks2 like Tasks;
  117.  
  118. insert Tasks2 select * from Tasks;
  119.  
  120. /*1-8:*/
  121.  
  122.  
  123. select * from Users where Name not like '%а' and Name not like '%a %';
  124.  
  125. select * from Users where login like 'p%' and login like '%r%';
  126.  
  127.  
  128.  
  129. /*2 deadline = 25.03
  130.   3 deadline = 08.04 */
  131.  
  132. /*TASK_2*/
  133.  
  134. /*2-1:*/
  135.  
  136. select exec_user_id from Tasks
  137. group by exec_user_id
  138. order by avg(Priority)
  139. limit 3;
  140.  
  141. /*2-2:*/
  142.  
  143. select concat(count(*),' - ', month(creat_date), ' - ', exec_user_id) from Tasks
  144. where year(creat_date) = 2016
  145. group by exec_user_id, month(creat_date);
  146.  
  147. /*2-3:*/
  148.  
  149. /* ??? */
  150.  
  151. /*2-4:*/
  152.  
  153. select distinct concat(creat_user_id, ' - ', exec_user_id)
  154. from Tasks
  155. group by creat_user_id, exec_user_id;
  156.  
  157. /*2-5:*/
  158.  
  159. select login from Users
  160. order by length(login)
  161. desc limit 1;
  162.  
  163. /*2-6:*/
  164.  
  165. SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';
  166.  
  167. drop table test
  168. Create table test
  169. (c CHAR(10),
  170. v VARCHAR(10));
  171.  
  172. Insert into test
  173. values('asd','asdasd');
  174.  
  175. select length(c) as 'CHAR', length(v) as 'VARCHAR' from test;
  176.  
  177. /*2-7:*/
  178.  
  179. select Users.Name, max(Tasks.Priority)
  180. from Users, Tasks
  181. where Users.id = Tasks.exec_user_id
  182. group by Users.id;
  183.  
  184. /*2-8:*/
  185.  
  186. select creat_user_id, sum(Elapsed_time) from Tasks
  187. where Priority > (select avg(Priority) from Tasks)
  188. group by creat_user_id;
  189.  
  190. /*2-9:*/
  191. drop view taskz;
  192.  
  193. create view taskz as
  194. select exec_user_id,
  195. count(id) as total_tasks
  196. from Tasks
  197. group by exec_user_id;
  198.  
  199. select * from taskz;
  200.  
  201.  
  202.  
  203.  
  204.  
  205. drop view taskz2;
  206.  
  207. create view taskz2 as
  208. select exec_user_id,
  209. sum(Evaluation > Elapsed_time) as in_time
  210. from Tasks
  211. where Evaluation is not null and Elapsed_time is not null
  212. group by exec_user_id;
  213.  
  214. select * from taskz2;
  215.  
  216.  
  217.  
  218.  
  219. drop view taskz3;
  220.  
  221. create view taskz3 as
  222. select exec_user_id,
  223. sum(Evaluation < Elapsed_time) as not_in_time
  224. from Tasks
  225. where Evaluation is not null and Elapsed_time is not null
  226. group by exec_user_id;
  227.  
  228. select * from taskz3;
  229.  
  230.  
  231.  
  232. drop view taskz4;
  233.  
  234. create view taskz4 as
  235. select exec_user_id,
  236. sum((task_Condition <=> 'Переоткрыта') or (task_Condition <=>'Новая')) as otkrita,
  237. sum(task_Condition <=> 'Закрыта') as zakr,
  238. sum(task_Condition <=> 'Выполняется') as vipoln
  239. from Tasks
  240. group by exec_user_id;
  241.  
  242. select * from taskz4;
  243.  
  244.  
  245.  
  246. drop view taskz4;
  247.  
  248. create view taskz4 as
  249. select exec_user_id,
  250. sum(Evaluation) as summ_potr_vremya,
  251. sum(Evaluation - Elapsed_time) as pererab
  252. from Tasks
  253. where Evaluation is not null and Elapsed_time is not null
  254. group by exec_user_id;
  255.  
  256. select * from taskz4;
  257.  
  258.  
  259.  
  260.  
  261. drop view taskz4;
  262.  
  263. create view taskz4 as
  264. select exec_user_id,
  265. sum(length(Description)) as descr_lengt,
  266. avg(Evaluation - Elapsed_time) as avg_pererab,
  267. max(Elapsed_time) as max_Elapsed_time
  268. from Tasks
  269. where Evaluation is not null and Elapsed_time is not null and Description is not null
  270. group by exec_user_id;
  271.  
  272. select * from taskz4;
  273.  
  274.  
  275.    
  276.  
  277.  
  278. /*2-10:*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement