Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.97 KB | None | 0 0
  1. drop table if exists
  2.     weather.measures;
  3.  
  4. create table weather.measures(
  5.     m_id int unsigned auto_increment,
  6.     m_name varchar(50) not null,
  7.     m_metric varchar(10) not null,
  8.    
  9.     primary key(m_id)
  10. );
  11.  
  12.  
  13. drop table if exists
  14.     weather.stantions;
  15.  
  16. create table weather.stantions(
  17.     s_id int unsigned auto_increment not null,
  18.     s_name varchar(50) not null,
  19.     s_city varchar(50) not null,
  20.    
  21.     primary key(s_id)
  22. );
  23.  
  24.  
  25. drop table if exists
  26.     weather.observations;
  27.  
  28.  
  29. create table observations(
  30.     obs_id int unsigned auto_increment not null,
  31.     meas_type int unsigned not null,
  32.     stantion_id int unsigned not null,
  33.     measure_value decimal(10, 2),
  34.     obs_date date not null,
  35.    
  36.     primary key(obs_id),
  37.    
  38.     foreign key(meas_type) references measures(m_id) on delete cascade,
  39.     foreign key(stantion_id) references stantions(s_id) on delete cascade
  40. );
  41.  
  42. insert into measures values
  43.     (1,'Давление', 'pas'),
  44.     (2,'Температура', 'С'),
  45.     (3,'Влажность', '%'),
  46.     (4,'Сила', 'Н');
  47.  
  48. insert into stantions values
  49.     (1, 'Восточная', 'Екатеринбург'),
  50.     (2, 'Sunny', 'Berlin'),
  51.     (3, 'Ботаническая', 'Москва');
  52.  
  53.    
  54. insert into observations values
  55.     (1, 1, 2, 23.45, curdate()),
  56.     (2, 3, 1, 55, curdate()),
  57.     (3, 1, 2, 77.3, curdate()),
  58.     (4, 1, 3, 33.34, curdate());
  59.    
  60. insert into observations values
  61.     (5, 2, 1, 23, curdate());
  62.    
  63. select  
  64.        s_name as 'Имя Станции',
  65.        s_city as 'Город Станции',
  66.        m_name as 'Физ. величина',
  67.        m_metric as 'Единица измерения',
  68.        format(avg(measure_value), 2) as 'Среднее значение',
  69.        date_format(obs_date, '%d.%m.%Y') as 'Дата'
  70.     from observations as obs
  71.         inner join stantions as st
  72.             on obs.stantion_id = st.s_id
  73.         inner join measures as ms
  74.             on obs.meas_type = ms.m_id
  75.            
  76.     group by meas_type, s_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement