Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.21 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.  
  142. select avg(Priority) from Tasks;
  143.  
  144. select avg(Priority) from Tasks
  145. group by id;
  146.  
  147. select avg(Priority) from Tasks
  148. group by null;
  149.  
  150.  
  151.  
  152. /*2-2:*/
  153.  
  154. select concat(count(*),' - ', month(creat_date), ' - ', exec_user_id) from Tasks
  155. where year(creat_date) = 2016
  156. group by exec_user_id, month(creat_date);
  157.  
  158. /*2-3:*/
  159.  
  160. select exec_user_id, sum(Elapsed_time-Evaluation), sum(Evaluation- Elapsed_time)from Tasks
  161. group by id;
  162.  
  163.  
  164. select  
  165. A.exec_user_id,
  166. A.summ, B.summ
  167. from (select exec_user_id, sum(Evaluation- Elapsed_time) as summ
  168.     from Tasks where Evaluation>Elapsed_time
  169.     group by exec_user_id) as A  
  170. join (select exec_user_id, sum( Elapsed_time-Evaluation) as summ
  171.     from Tasks where Evaluation<Elapsed_time
  172.     group by exec_user_id) as B
  173. on A.exec_user_id = B.exec_user_id;
  174.  
  175.  
  176.  
  177. /*2-4:*/
  178.  
  179. select distinct
  180. if( creat_user_id<exec_user_id, concat(creat_user_id, ' - ', exec_user_id), concat(exec_user_id, ' - ', creat_user_id) )
  181. from Tasks;
  182.  
  183. /*2-5:*/
  184.  
  185. select login from Users
  186. order by length(login)
  187. desc limit 1;
  188.  
  189. /*2-6:*/
  190.  
  191. SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';
  192.  
  193. drop table test
  194. Create table test
  195. (c CHAR(10),
  196. v VARCHAR(10));
  197.  
  198. Insert into test
  199. values('asd','asdasd');
  200.  
  201. select length(c) as 'CHAR', length(v) as 'VARCHAR' from test;
  202.  
  203. /*2-7:*/
  204.  
  205. select Users.Name, max(Tasks.Priority)
  206. from Users, Tasks
  207. where Users.id = Tasks.exec_user_id
  208. group by Users.id;
  209.  
  210. /*2-8:*/
  211.  
  212. select creat_user_id, sum(Elapsed_time) from Tasks
  213. where Priority > (select avg(Priority) from Tasks)
  214. group by creat_user_id;
  215.  
  216. /*2-9:*/
  217. drop view taskz;
  218.  
  219. create view taskz as
  220. select exec_user_id,
  221. count(*) as total_tasks
  222. from Tasks
  223. group by exec_user_id;
  224.  
  225. select * from taskz;
  226.  
  227.  
  228.  
  229.  
  230. drop view taskz2;
  231.  
  232. create view taskz2 as
  233. select exec_user_id,
  234. sum(Evaluation > Elapsed_time) as in_time
  235. from Tasks
  236. where Evaluation is not null and Elapsed_time is not null
  237. group by exec_user_id;
  238.  
  239. select * from taskz2;
  240.  
  241.  
  242.  
  243.  
  244. drop view taskz3;
  245.  
  246. create view taskz3 as
  247. select exec_user_id,
  248. sum(Evaluation < Elapsed_time) as not_in_time
  249. from Tasks
  250. where Evaluation is not null and Elapsed_time is not null
  251. group by exec_user_id;
  252.  
  253. select * from taskz3;
  254.  
  255.  
  256.  
  257. drop view taskz4;
  258.  
  259. create view taskz4 as
  260. select exec_user_id,
  261. sum((task_Condition = 'Переоткрыта') or (task_Condition ='Новая')) as otkrita,
  262. sum(task_Condition = 'Закрыта') as zakr,
  263. sum(task_Condition = 'Выполняется') as vipoln
  264. from Tasks
  265. group by exec_user_id;
  266.  
  267. select * from taskz4;
  268.  
  269.  
  270.  
  271. drop view taskz4;
  272.  
  273. create view taskz4 as
  274. select exec_user_id,
  275. sum(Evaluation) as summ_potr_vremya,
  276. sum(Evaluation - Elapsed_time) as pererab
  277. from Tasks
  278. where Evaluation is not null and Elapsed_time is not null
  279. group by exec_user_id;
  280.  
  281. select * from taskz4;
  282.  
  283.  
  284.  
  285.  
  286. drop view taskz4;
  287.  
  288. create view taskz4 as
  289. select exec_user_id,
  290. sum(length(Description)) as descr_lengt,
  291. avg(Evaluation - Elapsed_time) as avg_pererab,
  292. max(Elapsed_time) as max_Elapsed_time
  293. from Tasks
  294. where Evaluation is not null and Elapsed_time is not null and Description is not null
  295. group by exec_user_id;
  296.  
  297. select * from taskz4;
  298.  
  299.  
  300.    
  301.  
  302.  
  303. /*2-10:*/
  304.  
  305.  
  306. select Name, Title from Users, Tasks where Users.id = exec_user_id;
  307.  
  308.  
  309. select Name,id from Users where Users.id not in (select exec_user_id from Tasks);
  310.  
  311.  
  312. select Name, ifnull((select count(*) from Tasks where creat_user_id = Users.id group by creat_user_id ),0) as num_of_tasks from Users;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement