Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.15 KB | None | 0 0
  1. USE master
  2. GO
  3.  
  4. IF  EXISTS (
  5.         SELECT name
  6.                 FROM sys.databases
  7.                 WHERE name = N'AlexanderDubikovsky'
  8. )
  9. ALTER DATABASE AlexanderDubikovsky set single_user with rollback immediate
  10. GO
  11. DROP DATABASE AlexanderDubikovsky
  12. GO
  13.  
  14. CREATE DATABASE AlexanderDubikovsky
  15. GO
  16.  
  17. USE AlexanderDubikovsky
  18. GO
  19.  
  20. IF OBJECT_ID('Meteostations', 'U') IS NOT NULL
  21.         DROP TABLE Meteostations
  22. GO
  23.  
  24. CREATE TABLE Meteostations
  25. (
  26.         StationID varchar(10) NOT NULL,
  27.         StationName varchar(50) NULL,
  28.         StationAddress varchar(100) NULL,
  29.         CONSTRAINT PK_StationID PRIMARY KEY (StationID)
  30. )
  31. GO
  32.  
  33. IF OBJECT_ID('MeasuringTypes', 'U') IS NOT NULL
  34.         DROP TABLE MeasuringTypes
  35. GO
  36.  
  37. CREATE TABLE MeasuringTypes
  38. (      
  39.         TypeID varchar(10) NOT NULL,
  40.         Quantity varchar(30) NULL,
  41.         Unit varchar(10) NULL,
  42.         CONSTRAINT PK_Type_ID PRIMARY KEY (TypeID)
  43. )
  44. GO
  45.  
  46. IF OBJECT_ID('Measurings', 'U') IS NOT NULL
  47.         DROP TABLE Measurings
  48. GO
  49.  
  50. CREATE TABLE Measurings
  51. (
  52.         MStation varchar(10) NOT NULL,
  53.         MType varchar(10) NOT NULL,
  54.         MDate date NULL,
  55.         MValue int NOT NULL,
  56.         CONSTRAINT FK_Meteostations FOREIGN KEY (MStation)
  57.         REFERENCES Meteostations(StationID),
  58.         CONSTRAINT FK_MeasuringTypes FOREIGN KEY (MType)
  59.         REFERENCES MeasuringTypes(TypeID)
  60. )
  61. GO
  62.  
  63. INSERT INTO Meteostations
  64.         VALUES
  65.         ('1', 'Lillehammer-Saetherengen', 'Norway'),
  66.         ('2', 'Innsbruck-Flughafen', 'Austria'),
  67.         ('3', 'Became Station 146540', 'Serbia'),
  68.         ('4', 'Matsuyama', 'Japan'),
  69.         ('5', 'Zadar Puntamika', 'Croatia'),
  70.         ('6', N'Остров Преображения', 'Russia')
  71. GO
  72.  
  73. INSERT INTO MeasuringTypes
  74.         VALUES
  75.         ('1', N'Температура воздуха', 'С°'),
  76.         ('2', N'Атмосферное давление', 'мм рт.ст.'),
  77.         ('3', N'Скорость ветра', 'м/с'),
  78.         ('4', N'Влажность', '%')
  79. GO
  80.  
  81. INSERT INTO Measurings
  82.         VALUES
  83.         ('1', '1', '20.09.2014', 15),
  84.         ('1', '2', '20.09.2014', 723),
  85.         ('4', '3', '20.09.2014', 4),
  86.         ('3', '1', '20.09.2014', 24),
  87.         ('1', '1', '20.09.2014', 17),
  88.         ('1', '4', '20.09.2014', 63),
  89.         ('6', '1', '20.09.2014', 21),
  90.         ('4', '2', '20.09.2014', 734),
  91.         ('1', '4', '20.09.2014', 77),
  92.         ('3', '3', '20.09.2014', 4),
  93.         ('5', '1', '20.09.2014', 8),
  94.         ('6', '4', '20.09.2014', 34),
  95.         ('4', '1', '20.09.2014', 9),
  96.         ('2', '2', '20.09.2014', 754),
  97.         ('3', '3', '20.09.2014', 7),
  98.         ('2', '1', '20.09.2014', 19),
  99.         ('1', '1', '21.09.2014', 24),
  100.         ('5', '2', '21.09.2014', 715),
  101.         ('4', '2', '21.09.2014', 751),
  102.         ('1', '1', '21.09.2014', 20),
  103.         ('6', '4', '21.09.2014', 66),
  104.         ('2', '4', '21.09.2014', 57),
  105.         ('1', '1', '21.09.2014', 20),
  106.         ('5', '3', '21.09.2014', 1),
  107.         ('4', '3', '21.09.2014', 8),
  108.         ('3', '2', '21.09.2014', 65),
  109.         ('1', '1', '21.09.2014', 18),
  110.         ('5', '3', '21.09.2014', 3),
  111.         ('3', '2', '21.09.2014', 43)
  112. GO
  113.  
  114. SELECT Meteostations.StationName AS N'Станция', CONVERT(varchar(25), Measurings.MDate, 104) AS 'Дата',
  115.     AVG(Measurings.MValue) AS 'Среднее значение', MeasuringTypes.Unit AS 'Единица измерения'
  116. FROM Measurings
  117. INNER JOIN Meteostations
  118. ON Measurings.MStation=Meteostations.StationID
  119. INNER JOIN MeasuringTypes
  120. ON Measurings.MType=MeasuringTypes.TypeID
  121. GROUP BY Meteostations.StationName, MeasuringTypes.Unit, Measurings.MDate
  122. ORDER BY Measurings.MDate, Meteostations.StationName, MeasuringTypes.Unit
  123. GO
  124.  
  125. SELECT Meteostations.StationName AS N'Станция', AVG(Measurings.MValue) AS 'Среднее значение в данный день', MeasuringTypes.Unit AS 'Единица измерения'
  126. FROM Measurings
  127. INNER JOIN Meteostations
  128. ON Measurings.MStation=Meteostations.StationID
  129. INNER JOIN MeasuringTypes
  130. ON Measurings.MType=MeasuringTypes.TypeID
  131. WHERE Measurings.MDate = '20.09.2014'
  132. GROUP BY Meteostations.StationName, MeasuringTypes.Unit
  133. ORDER BY Meteostations.StationName, MeasuringTypes.Unit
  134. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement