exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 102 discussion

Actual exam question from Microsoft's 70-761
Question #: 102
Topic #: 1
[All 70-761 Questions]

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 accepts a parameter of type geography and 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:
✑ Return the average normalized readings named AverageReading.
✑ Return the nearest mountain name named Mountain.
✑ Do not return any other columns.
✑ Exclude sensors for which no normalized reading exists.
Construct the query using the following guidelines:
✑ Use one part names to reference tables, columns and functions.
✑ Do not use parentheses unless required.
Define column headings using the AS keyword.

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.

Show Suggested Answer Hide Answer
Suggested Answer: Please see explanation
1 SELECT avg (normalizedreading) as AverageReading, location as Mountain
2 FROM GroundSensors
3 WHERE normalizedreading is not null
Note: On line 1 change to AverageReading and change to Mountain.

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
vermeilyn
Highly Voted 4 years, 11 months ago
SELECT AVG(normalizedreading) as AverageReading, Nearestmountain(location) AS Mountain FROM GroundSensors WHERE normalizedreading is not NULL GROUP BY Nearestmountain(location)
upvoted 15 times
...
safiullah
Highly Voted 5 years, 8 months ago
Hi, I believe the correct answer should be. SELECT avg(normalizedreading) as AverageReading, Nearestmountain(location) as mountain FROM GroundSensors WHERE normalizedreading is not NULL GROUP BY location Note: Since "AVG" is an aggregation based on location, therefore we need GROUP BY based on location. Also we will not display location but the mountain in the location using the scalar function nearest mountain.
upvoted 14 times
Dieter
5 years, 8 months ago
Make sure that the GROUP BY clause contains also the "Nearestmountain" function
upvoted 8 times
AshleyLiang
5 years, 8 months ago
Or simply: GROUP BY Mountain as the alias specified.
upvoted 1 times
imran
5 years, 6 months ago
YOU CANT USE ALIAS IN GROUP BY CLAUSE
upvoted 19 times
...
...
Jiacheng
5 years, 2 months ago
it doesn't matter, same location will get same result after function, so using group by location is fine too
upvoted 5 times
Anette
4 years, 11 months ago
group by location is not allowed. Type 'geography' is not comparable. TESTED
upvoted 6 times
...
...
...
daniel_yes23
5 years ago
GROUP BY location doesn't work, The type "geography" is not comparable. It cannot be used in the GROUP BY clause.
upvoted 5 times
Anette
4 years, 11 months ago
Yes, its true, so it cannot be this solution
upvoted 1 times
...
...
...
Vermonster
Most Recent 4 years, 4 months ago
Answer is SELECT AVG(NormalizedReading) as averagereading, dbo.NearestMountain(Location) as Mountain FROM #GroundSensors WHERE NormalizedReading IS NOT NULL GROUP BY dbo.NearestMountain(Location) Full test -------- CREATE FUNCTION dbo.NearestMountain(@geo GEOGRAPHY) RETURNS CHAR(20) AS BEGIN DECLARE @text CHAR(20) = 'MtTest' RETURN @text END GO CREATE TABLE #GroundSensors( SensorID INT PRIMARY KEY, Location GEOGRAPHY NOT NULL, Tremor INT NOT NULL, NormalizedReading FLOAT NULL) INSERT INTO #GroundSensors VALUES(1,geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326),1,123.33), (2,geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326),1,343.2) GO SELECT AVG(NormalizedReading) as averagereading, dbo.NearestMountain(Location) as Mountain FROM #GroundSensors WHERE NormalizedReading IS NOT NULL GROUP BY dbo.NearestMountain(Location) DROP TABLE #GroundSensors DROP FUNCTION dbo.NearestMountain
upvoted 4 times
...
vramky
4 years, 4 months ago
SELECT AVG(NormalizedReading) AS AverageReading, NearestMountain(SensorID) AS Mountain FROM GroundSensors WHERE Tremor <>0 AND NormalizedReading IS NOT NULL GROUPBY NearestMountain(SensorID); GROUP by NearestMountain(SensorID)
upvoted 1 times
...
kiri2020
4 years, 6 months ago
Group by has to be using function, for the case several Locations pointing to same Mountain : select AVG( NormalizedReading) as AverageReading, dbo.NearestMountain(Location) as Mountain from GroundSensors where NormalizedReading is not null group by dbo.NearestMountain(Location)
upvoted 1 times
...
CristianCruz
4 years, 9 months ago
SELECT AVG(NormalizedReading), NearestMountain(SensorID) FROM GroundSensors WHERE NormalizedReading IS NOT NULL GROUP BY NearestMountain(SensorID)
upvoted 1 times
Michael93
4 years, 8 months ago
Nearest mountain only accepts parameter type geography, not int.
upvoted 1 times
...
...
mamarach
4 years, 10 months ago
SELECT avg(normalizedreading) as AverageReading, Nearestmountain(location) as Mountain FROM GroundSensors Group by Nearestmountain Having normalizedreading not NULL
upvoted 2 times
...
AlinaS
5 years ago
SELECT AVG(NormalizedReading) AS AverageReading, Mountain FROM (SELECT NnormalizedReading, NearestMountain(location) as Mountain FROM GroundSensors WHERE NormalizedReading is not NULL) AS t GROUP BY Mountain;
upvoted 3 times
...
mlourinho
5 years, 5 months ago
SELECT dbo.NearestMountain(g.Location) as Mountain , avg(g.NormalizedReading) as AverageReading FROM GroundSensor g where g.NormalizedReading is not null group by g.Location
upvoted 1 times
...
M4x
5 years, 8 months ago
select NearestMountain(Location) AS Mountain, AVG(Tremor) AS AverageReading from GroundSensors where NormalizedReading is not null group by NearestMountain(Location)
upvoted 7 times
anonimdom
5 years, 3 months ago
Why AVG(Tremor) instead of avg(normalizedreading)?
upvoted 4 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago