Advertisement
Guest User

Untitled

a guest
Oct 20th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 7.82 KB | None | 0 0
  1. Gmail   Atnes Ness <atnesness@gmail.com>
  2. (no subject)
  3. Atnes Ness <atnesness@gmail.com>    14 October 2015 at 16:11
  4. TO: galinka95.95@mail.ru
  5.  
  6. ---------- Forwarded message ----------
  7. FROM: Atnes Ness <atnesness@gmail.com>
  8. DATE: 7 October 2015 at 17:39
  9. Subject:
  10. TO: Atnes Ness <atnesness@gmail.com>
  11.  
  12.  
  13. CREATE DATABASE [Nesmianov.Artem]
  14.  
  15. GO
  16.  
  17. USE [Nesmianov.Artem];
  18.  
  19. GO
  20.  
  21. CREATE TABLE measure_table (
  22.  
  23. id INTEGER PRIMARY KEY,
  24.  
  25. name VARCHAR(100) );
  26.  
  27.  
  28.  
  29. CREATE TABLE station_table (
  30.  
  31. id INTEGER PRIMARY KEY,
  32.  
  33. name VARCHAR(100));
  34.  
  35.  
  36.  
  37. CREATE TABLE stat_measure_table (
  38.  
  39. id INTEGER PRIMARY KEY,
  40.  
  41. datestamp DATE,
  42.  
  43. DATA FLOAT,
  44.  
  45. station_id INTEGER,
  46.  
  47. measure_id INTEGER );
  48.  
  49. ALTER TABLE stat_measure_table
  50.  
  51.  ADD CONSTRAINT fk_station
  52.  
  53.  FOREIGN KEY (station_id)
  54.  
  55.  REFERENCES station_table(id);
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. ALTER TABLE stat_measure_table
  64.  
  65.  ADD CONSTRAINT fk_measure
  66.  
  67.  FOREIGN KEY (measure_id)
  68.  
  69.  REFERENCES measure_table(id);
  70.  
  71. ALTER TABLE stat_measure_table
  72.  
  73. ADD timestmp TIME;
  74.  
  75.  
  76.  
  77. ALTER TABLE station_table ADD addr VARCHAR(100)
  78.  
  79.  DEFAULT 'Россия, г.';
  80.  
  81.  
  82.  
  83. INSERT INTO station_table (id, name)
  84.  
  85. VALUES (0, N'station1');
  86.  
  87. INSERT INTO station_table (id, name)
  88.  
  89. VALUES (1, 'station2');
  90.  
  91.  
  92.  
  93. INSERT INTO station_table (id, name)
  94.  
  95. VALUES (2, 'station3');
  96.  
  97.  
  98.  
  99. INSERT INTO station_table (id, name)
  100.  
  101. VALUES (3, 'station4');
  102.  
  103.  
  104.  
  105. INSERT INTO station_table (id, name)
  106.  
  107. VALUES (4, 'station5');
  108.  
  109. INSERT INTO station_table (id, name)
  110.  
  111. VALUES (5, 'station6');
  112.  
  113.  
  114.  
  115. INSERT INTO station_table (id, name)
  116.  
  117. VALUES (6, 'station7');
  118.  
  119.  
  120.  
  121. INSERT INTO station_table (id, name)
  122.  
  123. VALUES (7, 'station8');
  124.  
  125.  
  126.  
  127. INSERT INTO station_table (id, name)
  128.  
  129. VALUES (8, 'station9');
  130.  
  131.  
  132.  
  133. INSERT INTO station_table (id, name)
  134.  
  135. VALUES (9, 'station10');
  136.  
  137.  
  138.  
  139.  
  140.  
  141. INSERT INTO measure_table (id, name)
  142.  
  143. VALUES (0, 'Давление');
  144.  
  145. INSERT INTO measure_table (id, name)
  146.  
  147. VALUES (1, 'Температура');
  148.  
  149. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  150.  
  151. VALUES (0, 0, 0, '2015-08-12', '12:00', 111);
  152.  
  153. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  154.  
  155. VALUES (1, 1, 0, '2015-08-12', '12:00', 24);
  156.  
  157. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  158.  
  159. VALUES (2, 0, 1, '2015-08-12', '13:00', 130);
  160.  
  161. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  162.  
  163. VALUES (3, 1, 1, '2015-08-12', '13:00', 27);
  164.  
  165. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  166.  
  167. VALUES (4, 0, 2, '2015-08-12', '14:00', 101);
  168.  
  169. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  170.  
  171. VALUES (5, 1, 2, '2015-08-12', '14:00', 30);
  172.  
  173. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  174.  
  175. VALUES (6, 0, 2, '2015-08-12', '15:00', 98);
  176.  
  177. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  178.  
  179. VALUES (7, 1, 2, '2015-08-12', '15:00', 21);
  180.  
  181. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  182.  
  183. VALUES (8, 0, 2, '2015-08-12', '16:00', 111);
  184.  
  185. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  186.  
  187. VALUES (9, 1, 2, '2015-08-12', '16:00', 25);
  188.  
  189. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  190.  
  191. VALUES (10, 0, 2, '2015-08-12', '17:00', 110);
  192.  
  193. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  194.  
  195. VALUES (11, 1, 2, '2015-08-12', '17:00', 32);
  196.  
  197. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  198.  
  199. VALUES (12, 0, 2, '2015-08-12', '18:00', 100);
  200.  
  201. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  202.  
  203. VALUES (13, 1, 2, '2015-08-12', '18:00', 28);
  204.  
  205. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  206.  
  207. VALUES (14, 0, 2, '2015-08-12', '19:00', 110);
  208.  
  209. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  210.  
  211. VALUES (15, 1, 2, '2015-08-12', '19:00', 24);
  212.  
  213. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  214.  
  215. VALUES (16, 0, 2, '2015-08-12', '20:00', 98);
  216.  
  217. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  218.  
  219. VALUES (17, 1, 2, '2015-08-12', '20:00', 20);
  220.  
  221. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  222.  
  223. VALUES (18, 0, 2, '2015-08-12', '21:00', 95);
  224.  
  225. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  226.  
  227. VALUES (19, 1, 2, '2015-08-12', '21:00', 18);
  228.  
  229.  
  230.  
  231.  
  232.  
  233. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  234.  
  235. VALUES (20, 0, 2, '2015-08-13', '18:00', 100);
  236.  
  237. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  238.  
  239. VALUES (21, 1, 2, '2015-08-14', '18:00', 28);
  240.  
  241. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  242.  
  243. VALUES (22, 0, 2, '2015-08-13', '19:00', 110);
  244.  
  245. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  246.  
  247. VALUES (23, 1, 2, '2015-08-13', '19:00', 24);
  248.  
  249. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  250.  
  251. VALUES (24, 0, 2, '2015-08-13', '20:00', 98);
  252.  
  253. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  254.  
  255. VALUES (25, 1, 2, '2015-08-14', '20:00', 20);
  256.  
  257. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  258.  
  259. VALUES (26, 0, 2, '2015-08-14', '21:00', 95);
  260.  
  261. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  262.  
  263. VALUES (27, 1, 2, '2015-08-13', '21:00', 18);
  264.  
  265.  
  266.  
  267. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  268.  
  269. VALUES (28, 1, 2, '2015-08-13', '21:00', 18);
  270.  
  271. INSERT INTO stat_measure_table (id, measure_id, station_id, datestamp, timestmp, DATA)
  272.  
  273. VALUES (29, 0, 2, '2015-08-13', '21:00', 18);
  274.  
  275.  
  276.  
  277.  CREATE TABLE measure_type_table (
  278.  
  279. id INTEGER PRIMARY KEY,
  280.  
  281. name VARCHAR(10)
  282.  
  283.  );
  284.  
  285. INSERT INTO measure_type_table (id, name)
  286.  
  287. VALUES (0, 'C');
  288.  
  289. INSERT INTO measure_type_table (id, name)
  290.  
  291. VALUES (1, 'Па');
  292.  
  293. ALTER TABLE measure_type_table
  294.  
  295. ADD measure_id INTEGER;
  296.  
  297.  
  298.  
  299. ALTER TABLE measure_type_table
  300.  
  301.  ADD CONSTRAINT fk_tmeasure
  302.  
  303.  FOREIGN KEY (measure_id)
  304.  
  305.  REFERENCES measure_table(id);
  306.  
  307.  SELECT datestamp FROM stat_measure_table GROUP BY datestamp;
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  SELECT measure_table.name AS 'Измерение', MAX(DATA) AS 'MAX', MIN(DATA) AS 'MIN'
  314.  
  315.  FROM stat_measure_table LEFT JOIN measure_table
  316.  
  317.     ON measure_table.id = stat_measure_table.measure_id
  318.  
  319.     GROUP BY (measure_table.name)
  320.  
  321.  
  322.  
  323. UPDATE measure_type_table SET measure_id = 1 WHERE id = 0;
  324.  
  325. UPDATE measure_type_table SET measure_id = 0 WHERE id = 1;
  326.  
  327. SELECT * FROM measure_type_table;
  328.  
  329. SELECT DATA AS 'C' FROM stat_measure_table WHERE measure_id IN (
  330.  
  331.     SELECT measure_id FROM measure_type_table WHERE measure_table.id = 0
  332.  
  333. );
  334.  
  335. SELECT name AS 'измерение' , DATA AS 'данные', datestamp AS 'дата'
  336.  
  337. FROM stat_measure_table LEFT JOIN measure_table
  338.  
  339.     ON stat_measure_table.measure_id = measure_table.id WHERE DATA IN (
  340.  
  341.     SELECT MIN(DATA) FROM stat_measure_table GROUP BY(measure_id)
  342.  
  343.     )
  344.  
  345.  
  346.  
  347. SELECT datestamp AS 'дата', measure_table.name AS 'измерение' ,
  348.  
  349.  AVG(DATA) AS 'ср знач', measure_type_table.name AS 'ед изм'
  350.  
  351.  FROM
  352.  
  353. stat_measure_table LEFT JOIN measure_table
  354.  
  355.     ON stat_measure_table.measure_id = measure_table.id
  356.  
  357.     LEFT JOIN measure_type_table
  358.  
  359.     ON measure_table.id = measure_type_table.measure_id
  360.  
  361.     GROUP BY datestamp, measure_table.name, measure_type_table.name;
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement