exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 44 discussion

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

SIMULATION -
You have a table named Cities that has the following two columns: CityID and CityName. The CityID column uses the int data type, and CityName uses nvarchar
(max).
You have a table named RawSurvey. Each row includes an identifier for a question and the number of persons that responded to that question from each of four cities. The table contains the following representative data:

A reporting table named SurveyReport has the following columns: CityID, QuestionID, and RawCount, where RawCount is the value from the RawSurvey table.
You need to write a Transact-SQL query to meet the following requirements:
✑ Retrieve data from the RawSurvey table in the format of the SurveyReport table.
✑ The CityID must contain the CityID of the city that was surveyed.
✑ The order of cities in all SELECT queries must match the order in the RawSurvey table.
✑ The order of cities in all IN statements must match the order in the RawSurvey table.
Construct the query using the following guidelines:
✑ Use one-part names to reference tables and columns, except where not possible.
✑ ALL SELECT statements must specify columns.
✑ Do not use column or table aliases, except those provided.
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

UNPIVOT must be used to rotate columns of the Rawsurvey table into column values.
References:
https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

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
raf77
Highly Voted 5 years, 8 months ago
The correct answer is: SELECT CityId, QuestionId, RawCount FROM RawSurvey AS t1 UNPIVOT (RawCount FOR CityName IN ('Tokyo', 'Boston', 'London', 'NewYork')) AS t2 JOIN Cities ON CityName = CityName;
upvoted 21 times
Tazul
5 years, 7 months ago
----- IN (Tokyo, Boston, London, [New York])
upvoted 10 times
mlourinho
5 years, 6 months ago
SELECT CityId, QuestionId, RawCount FROM #RawSurvey AS t1 UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, [New York])) AS t2 JOIN #Cities C ON t2.CityName = C.CityName;
upvoted 6 times
kiri2020
4 years, 7 months ago
it says- Do not use column or table aliases, except those provided. - can't do Cities as C. Why to give alias to #RawSurvey if that alias is not used ? Should be : SELECT CityID, QuestionID, RawCount FROM RawSurvey UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, [New York])) as t1 JOIN Cities as t2 ON t1.CityName = t2.CityName
upvoted 2 times
...
...
AnsB
5 years, 3 months ago
Tazul is RIGHT. No quotation marks here for column names
upvoted 2 times
...
Aghie
4 years, 9 months ago
DECLARE @Cities TABLE(CityID int, CityName nvarchar(max)) INSERT INTO @Cities SELECT 1, 'Tokyo' INSERT INTO @Cities SELECT 2, 'Boston' INSERT INTO @Cities SELECT 3, 'London' INSERT INTO @Cities SELECT 4, 'New York' DECLARE @RawSurvey TABLE(QuestionID nvarchar(2), Tokyo int, Boston int, London int, [New York] int) INSERT INTO @RawSurvey SELECT 'Q1', 1, 42, 48, 51 INSERT INTO @RawSurvey SELECT 'Q2', 22, 39, 58, 42 INSERT INTO @RawSurvey SELECT 'Q3', 29, 41, 61, 33 INSERT INTO @RawSurvey SELECT 'Q4', 62, 70, 60, 50 INSERT INTO @RawSurvey SELECT 'Q5', 63, 31, 41, 21 INSERT INTO @RawSurvey SELECT 'Q6', 32, 1, 16, 34 SELECT CityID, QuestionID, RawCount FROM @RawSurvey AS t1 UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, [New York])) AS t2 JOIN @Cities c ON t2.CityName = c.CityName
upvoted 5 times
eduardogtc
4 years, 8 months ago
Tested and Worked
upvoted 1 times
...
...
...
Andy7622
4 years, 5 months ago
No single quotes are needed, cause these are column names not values.
upvoted 1 times
...
...
mlourinho
Highly Voted 5 years, 6 months ago
SELECT CityId, QuestionId, RawCount FROM ( SELECT QuestionId, Tokyo, Boston, London, [New York] FROM #RawSurvey ) AS t1 UNPIVOT ( RawCount FOR CityName IN (Tokyo, Boston, London, [New York]) ) AS t2 INNER JOIN Cities ON t2.CityName = Cities.CityName
upvoted 9 times
AnsB
5 years, 3 months ago
confirmed. RIGHT
upvoted 2 times
...
Anette
4 years, 12 months ago
yes its very RIGHT
upvoted 1 times
...
Anette
4 years, 12 months ago
but the thing is that it must be New York and NOT NewYork
upvoted 1 times
...
...
julie2020
Most Recent 4 years, 9 months ago
Select CityID, QuestionID, RawCount FROM (SELECT QuestionId, Tokyo, Boston, London, [New York] FROM RawSurvey) AS t1 UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, [New York]) AS t2 JOIN Cities ON t2.CityName = t1.CityName
upvoted 1 times
...
julie2020
4 years, 9 months ago
1 Select CityID, QuestionID, RawCount 2 FROM (SELECT QuestionId, Tokyo, Boston, London, [NewYork] FROM RawSurvey) AS t1 3 UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, [NewYork]) AS t2 4 JOIN Cities ON t2.CityName = t1.CityName
upvoted 2 times
...
julie2020
4 years, 9 months ago
1 Select CityID, QuestionID, RawCount 2 FROM (SELECT QuestionId, Tokyo, Boston, London, NewYork FROM RawSurvey) AS t1 3 UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, NewYork)) AS t2 4 JOIN Cities ON t2.CityName = t1.CityName
upvoted 3 times
...
MML
5 years ago
select CityId,QuestionId,RawCount from (select QuestionId, [Tokyo] as [1], [Boston] as [2], [London] as [3], [NewYork] as [4] from RawSurvey) as Src unpivot(Rawcount for CityId in ([1],[2],[3],[4])) as Unp
upvoted 1 times
...
Barbedx
5 years, 2 months ago
with Cities as ( select * from (values (1,'Tokyo' ), (2,'Boston' ), (3,'London' ), (4,'New York'))v(CityId,CityName)) ,RawSurvey as ( select * from (values ('Q1',1, 42,48,51), ('Q2',22,39,58,42), ('Q3',29,41,61,33), ('Q4',62,70,60,50), ('Q5',63,31,41,21), ('Q6',32,1, 16,34))v(QuestionId, [Tokyo], [Boston], [London], [New York])) SELECT CityID, questionId, rawCount from RawSurvey as t1 unpivot (rawCount for City in( [Tokyo], [Boston], [London], [New York])) as t2 join Cities on CityName = t2.City
upvoted 1 times
MML
5 years ago
There is no keyword 'with' so you can't use CTE
upvoted 1 times
MarcusJB
4 years, 11 months ago
The purpose was just to have the example data from the question in your query to try it out on your own. The answer is just the last part.
upvoted 1 times
...
...
...
DEBRA
5 years, 3 months ago
select cityid, questionid, rawcount from (select questionid, tokyo, etc)) as t1 unpivot( rawcount for cityname in(tokyo, etc) as t2 join cities on t2.cityname = t1.cityname
upvoted 1 times
...
hgi
5 years, 4 months ago
my version (not tested) select CityID, QuestionID, RawCount from (select QuestionID, CityName, RawCount from Rawsurvey unpivot RawCount for CityName in (Tokyo, Boston, London, [New York]) as t1) as t2 join Cities on Cities.CityName = t2.CityName
upvoted 1 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