You need to create a query that shows the average of th…

SIMULATION
You work for an organization that monitors seismic activity around volcanos. You have a table named
GroundSensors. The table stored data collected from seismic sensors. It includes the columns describes in the
following table:

The database also contains a scalar value function named NearestMountain that returns the name of the
mountain that is nearest to the sensor.You need to create a query that shows the average of the normalized readings from the sensors for each
mountain. The query must meet the following requirements:
Include the average normalized readings and nearest mountain name.
Exclude sensors for which no normalized reading exists.
Exclude those sensors with value of zero for tremor.
Construct the query using the following guidelines:
Use one part names to reference tables, columns and functions.
Do not use parentheses unless required.
Do not use aliases for column names and table names.
Do not surround object names with square brackets.

Part of the correct Transact-SQL has been provided in the answer area below. Enter the code in the answer
area that resolves the problem and meets the stated goals or requirements. You can add code within the code
that has been provided as well as below it.

Use the Check Syntax button to verify your work. Any syntax or spelling errors will be reported by line and
character position.

SIMULATION
You work for an organization that monitors seismic activity around volcanos. You have a table named
GroundSensors. The table stored data collected from seismic sensors. It includes the columns describes in the
following table:

The database also contains a scalar value function named NearestMountain that returns the name of the
mountain that is nearest to the sensor.You need to create a query that shows the average of the normalized readings from the sensors for each
mountain. The query must meet the following requirements:
Include the average normalized readings and nearest mountain name.
Exclude sensors for which no normalized reading exists.
Exclude those sensors with value of zero for tremor.
Construct the query using the following guidelines:
Use one part names to reference tables, columns and functions.
Do not use parentheses unless required.
Do not use aliases for column names and table names.
Do not surround object names with square brackets.

Part of the correct Transact-SQL has been provided in the answer area below. Enter the code in the answer
area that resolves the problem and meets the stated goals or requirements. You can add code within the code
that has been provided as well as below it.

Use the Check Syntax button to verify your work. Any syntax or spelling errors will be reported by line and
character position.

Answer: See the explanation

Explanation:
SELECT Average(NormalizedReading), NearestMountain(SensorID)
FROM GroundSensors
GROUP BY NearestMountain(SensorID)
WHERE TREMOR IS NOT 0 AND NormalizedReading IS NOT NULL

GROUP BY is a SELECT statement clause that divides the query result into groups of rows, usually for the
purpose of performing one or more aggregations on each group. The SELECT statement returns one row per
group.
https://msdn.microsoft.com/en-us/library/ms177673.aspx



Leave a Reply 6

Your email address will not be published. Required fields are marked *


fish

fish

Should use AVG() function. not Average()
SELECT AVG(NormalizedReading)

scotrid

scotrid

so many things wrong here 1) a where clause after a group by 2) is not 0 does this even exists ? 3) average ? really ? 4) grouping by a scalar ?

Peter

Peter

SELECT
dbo.NearestMountain(SensorID) AS MountainName,
AVG(NormalizedReading) AS NormalizedReading
FROM GroundSensors
WHERE Tremor 0
AND NormalizedReading IS NOT NULL
GROUP BY dbo.NearestMountain(SensorID);

will work as you may see from the example code below.

For the function a two part name is needed otherwise we run in the error message “‘NearestMountain’ is not a recognized built-in function name.”.

Sample code:
IF EXISTS (SELECT TOP(1) 42 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘dbo’ and TABLE_NAME = ‘GroundSensors’)
BEGIN
DROP TABLE dbo.GroundSensors;
END;
IF EXISTS (SELECT TOP(1) 42 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘dbo’ and TABLE_NAME = ‘ClosestMountain’)
BEGIN
DROP TABLE dbo.ClosestMountain;
END;
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NearestMountain]’) AND type IN ( N’FN’, N’IF’, N’TF’, N’FS’, N’FT’ ))
BEGIN
DROP FUNCTION [dbo].[NearestMountain]
END;

CREATE TABLE GroundSensors (SensorID INT PRIMARY KEY, Tremor INT NOT NULL, NormalizedReading float NULL);
CREATE TABLE ClosestMountain (MountainName NVARCHAR(20), SensorID INT);

INSERT INTO GroundSensors VALUES
(1,0,5),(2,5,7),(3,6,NULL),(4,2,4),(5,9,27);
INSERT INTO ClosestMountain VALUES
(‘Mountain1’,1),(‘Mountain1’,2),(‘Mountain1’,3),(‘Mountain2’,4),(‘Mountain2’,5);
GO

CREATE FUNCTION [dbo].[NearestMountain] (@SensorID INT)
RETURNS NVARCHAR(20)
AS
BEGIN
DECLARE @MountainName NVARCHAR(20);

SELECT @MountainName = MountainName
FROM ClosestMountain WHERE SensorID = @SensorID;

RETURN @MountainName;
END
GO

SELECT
dbo.NearestMountain(SensorID) AS MountainName,
AVG(NormalizedReading) AS NormalizedReading
FROM GroundSensors
WHERE Tremor 0
AND NormalizedReading IS NOT NULL
GROUP BY dbo.NearestMountain(SensorID);

scotrid

scotrid

@Peter sorry you got it wrong here. first no need to use schema and also you are including zero for tremor. answer should be

Select avg(normalizedreadings), NearestMountain(SensorID)
From GroundSensors
Where tremor ‘0’ and normalizedreading is not null
GROUP BY NearestMountain (SensorID)

EY Audtior

EY Audtior

Peter’s not equal to sign will be omitted on during submission on this site.

EY Audtior

EY Audtior

SELECT AVE(NormalizedReading), NearestMountain(SensorID)
FROM GroundSensors
WHERE NormalizedReading IS NOT NULL AND Tremor (NOT EQUAL TO) 0
GROUP BY NearestMountain(SensorID)