Advertisement
Stroce

Lab1

Oct 7th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.81 KB | None | 0 0
  1. use [Gallyam.Biktashev]
  2.  
  3. create table measurements
  4. (
  5.     id int not null identity(1,1),
  6.     measurement_value int,
  7.     measurement_date date,
  8.     measurement_time time,
  9.     primary key (id)
  10. )
  11. GO
  12.  
  13. create table stations (
  14.     id int not null identity(1,1),
  15.     name varchar(30) not null,
  16.     primary key (id)
  17. )
  18. GO
  19.  
  20. create table measurements_names (
  21.     id int not null identity(1,1),
  22.     name varchar(30) not null,
  23.     primary key (id)
  24. )
  25. GO
  26.  
  27. alter table measurements
  28. add station int,
  29. foreign key (station) references stations (id);
  30. GO
  31.  
  32. alter table measurements
  33. add measurement_type int,
  34. foreign key (measurement_type) references measurements_names (id);
  35. GO
  36.  
  37. create table measurement_units
  38. (
  39.     id int not null identity(1,1),
  40.     name varchar(40),
  41.     shortname varchar(10),
  42.     primary key(id)
  43. )
  44. GO
  45.  
  46. alter table stations
  47. add address varchar(200) default 'Россия, г.Верхушкино, ул. Пушкина, д. Колотушкина';
  48. GO
  49.  
  50. alter table measurements_names
  51. add unit int,
  52. foreign key (unit) references measurement_units(id);
  53. GO
  54.  
  55. insert into measurement_units
  56. values
  57.     ('Цельсий', 'ºC'),
  58.     ('Фаренгейт', 'F'),
  59.     ('Паскаль', 'Па');
  60. GO
  61.  
  62. insert into stations
  63. values
  64.     ('Окуловский', 'Россия, г.Екатеринбург, ул. Тургенева, д. 4'),
  65.     ('Ватников', 'Россия, г.Екатеринбург, ул. Мира, д. 19'),
  66.     ('Кремлевская', 'Россия, г.Москва, Красная площадь, д. 1');
  67. GO
  68.  
  69. insert into measurements_names
  70. values
  71.     ('Температура', 1),
  72.     ('Температура', 2),
  73.     ('Давление', 3);
  74. GO
  75.  
  76. insert into measurements
  77. values
  78.     (35, '5.07.1954', '12:24', 2, 2),
  79.     (123, '5.07.1954', '13:24', 2, 2),
  80.  
  81.     (35, '24.11.1988', '10:24', 1, 1),
  82.     (51234, '24.11.1988', '13:24', 1, 1),
  83.     (100000, '24.11.1988', '11:24', 1, 3),
  84.  
  85.     (214, '5.07.2000', '20:24', 3, 2),
  86.     (355, '5.07.2000', '19:24', 3, 2),
  87.  
  88.     (342, '5.07.2005', '17:24', 3, 1),
  89.     (-123, '5.07.1992', '16:24', 3, 2),
  90.     (-1234, '5.07.95', '18:24', 1, 3),
  91.     (312, '5.07.2004', '02:24', 2, 3),
  92.     (-35, '5.07.1945', '03:24', 3, 2);
  93. GO
  94.  
  95.  
  96. select distinct measurement_date from measurements order by measurement_date asc;
  97. GO
  98.  
  99. select measurement_type, max(measurement_value) as max_value, min(measurement_value) as min_value from measurements
  100.     group by measurement_type;
  101. GO
  102.  
  103. select *
  104. from measurements as m
  105. where m.measurement_type in(
  106.     select id
  107.     from measurements_names
  108.     where unit in (
  109.         select id
  110.         from measurement_units
  111.         where shortname = 'ºC'
  112.     )
  113. )
  114. GO
  115.  
  116. select m.measurement_type, m.measurement_date, m.measurement_value
  117. from measurements as m
  118. where m.measurement_value in (
  119.     select min(m1.measurement_value)
  120.     from measurements as m1
  121.     where m.measurement_type = m1.measurement_type
  122.     group by m1.measurement_type
  123. )
  124. GO
  125.  
  126. select measurement_name as 'Тип измерения', unit_name as 'Обозначение', station_name as 'Станция', avg(measurement_value) as 'Среднее значение', format(measurement_date, 'dd.MMMM.yy(yyyy)') as 'Дата измерения'
  127. from
  128.     (select mn.id as mn_id, mn.name as measurement_name, mu.shortname as unit_name
  129.         from measurements_names as mn
  130.         left join measurement_units as mu
  131.         on mn.unit = mu.id
  132.     ) as mn
  133.     right join measurements as m
  134.     on m.measurement_type = mn.mn_id
  135.     left join (select id, name as station_name from stations as s) as s
  136.     on s.id = m.station
  137. group by measurement_name, unit_name, station_name, measurement_date
  138. order by measurement_name, unit_name, station_name, measurement_date
  139.  
  140. select mo.measurement_date, mo.measurement_type, mo.station, mo.measurement_value
  141. from measurements as mo
  142. where mo.measurement_value > (
  143.     select avg(m.measurement_value)
  144.     from measurements as m
  145.     where m.measurement_type = mo.measurement_type
  146. )
  147. group by mo.measurement_type, mo.measurement_date, mo.station, mo.measurement_value
  148.  
  149. select
  150.  m.measurement_date as 'Дата измерения',
  151. measurements_names.name 'Измерение',
  152. stations.name as 'Станция',
  153. m.measurement_value as 'Значение',
  154. measurement_units.shortname as 'Единицы измерения'
  155.  
  156. from measurements as m
  157.  
  158.  inner join stations
  159.  on stations.id = m.station
  160.  
  161.  inner join measurements_names
  162.  on measurements_names.id = measurement_type
  163.  inner join measurement_units
  164.  on measurement_units.id = measurements_names.unit
  165. where m.measurement_value > (
  166.  select avg(measurement_value)
  167.  from measurements
  168.  where measurement_type = m.measurement_type
  169. )
  170. group by m.measurement_date, measurements_names.name, measurement_units.shortname, stations.name, m.measurement_value
  171. order by m.measurement_date, measurement_units.shortname
  172.  
  173. --drop table measurements
  174. --drop table stations
  175. --drop table measurements_names
  176. --drop table measurement_units
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement