exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 30 discussion

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

DRAG DROP -
Note: This question is part of a series of questions that use the same scenario. For your convenience, the scenario is repeated in each question. Each question presents a different goal and answer choices, but the text of the scenario is exactly the same in each question on this series.
You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables. The database contains the
Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.
Details for the Sales.Customers table are shown in the following table:

Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

You are creating a report to show when the first customer account was opened in each city. The report contains a line chart with the following characteristics:
✑ The chart contains a data point for each city, with lines connecting the points.
✑ The X axis contains the position that the city occupies relative to other cities.
✑ The Y axis contains the date that the first account in any city was opened.
An example chart is shown below for five cities:

During a sales promotion, customers from various cities open new accounts on the same date.
You need to write a query that returns the data for the chart.
How should you complete the Transact-SQL statement? To answer, drag the appropriate Transact-SQL segments to the correct locations. Each Transact-SQL segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.
NOTE: Each correct selection is worth one point.
Select and Place:

Show Suggested Answer Hide Answer
Suggested Answer:
Box 1: RANK() OVER -
RANK returns the rank of each row within the partition of a result set. The rank of a row is one plus thenumber of ranks that come before the row in question.
ROW_NUMBER and RANK are similar. ROW_NUMBER numbers all rows sequentially (for example 1, 2, 3, 4, 5).
Incorrect Answers:
DENSE_RANK returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
Box 2: (PARTITION BY CityID ORDER BY MIN(AccountOpenedDate) DESC)
Syntax for RANK: RANK ( ) OVER ( [ partition_by_clause ] order_by_clause )

Box 3: GROUP BY CityID -
References:
https://msdn.microsoft.com/en-us/library/ms176102.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
avramov
Highly Voted 5 years, 8 months ago
isn't it: Select CityID, min(AccountOpenedDate), DENSE_RANK() OVER(order by min(AccountOpenedDate) desc) From Application.Cities inner join Sales.Customer on CityID=PostalCityID group by CityID order by min(AccountOpenedDate) desc why needed a partition by?
upvoted 16 times
hgi
5 years, 5 months ago
correct, it should be without partition by, otherwise we don't compare with the other cities
upvoted 2 times
...
AnsB
5 years, 5 months ago
This is the CORRECT answer. NO partition by needed here
upvoted 2 times
...
tz_123
5 years ago
I agree, avramov is correct
upvoted 1 times
...
eduardogtc
4 years, 9 months ago
tested and avramov is correct. We don't need a partition here. 2 comments: 1)The option with OVER( order by AccountOpenedDate) gives a compilation error. We need to put min(AccountOpenedDate). 2) If we put PARTITION BY, all ranks are equal to 1
upvoted 2 times
...
...
Tazul
Highly Voted 5 years, 9 months ago
wondering why rank function needed??, one possible reason could be ranking the cities based on min(accountopendate) desc order. In that case, following fits the answer boxes. Select CityID, min(AccountOpenedDate), RANK() OVER(order by min(AccountOpenedDate) desc) From Application.Cities inner join Sales.Customer on CityID=PostalCityID group by CityID order by min(AccountOpenedDate) desc
upvoted 9 times
AnsB
5 years, 5 months ago
It should be DENSE_RANK here.
upvoted 5 times
...
...
Billybob0604
Most Recent 4 years, 5 months ago
Can anybody explain to me why t a. the DESC is used ? b. dense_rank would be wrong ? (there's only 1 date per city, so 1 rank number) c. why the partition is used ? (there's only 1 categorie you rank) There's a lot to this question which I don't understand
upvoted 1 times
...
Vermonster
4 years, 5 months ago
Given solution is fine. RANK() OVER(PARTITION BY City ORDER BY MIN(AccountOpened)) is correct. DENSE_RANK could yield multiple cities with same rank and not go to 6. GROUP BY required because of other aggregation
upvoted 1 times
...
julie2020
4 years, 11 months ago
NOTE: Each correct selection is worth one point. ? Means if two right answer from the three will still get point? How point works in this kind of questions ? If we get one wrong out of three.?
upvoted 1 times
...
Joeyboats
4 years, 12 months ago
Not sure what everyone means the solution is fine. Rank over partitions the city by lowest date. So if the cities that are lowest will be ranked 1
upvoted 1 times
Joeyboats
4 years, 12 months ago
Sorry 1 will be the newest date 2 will be second newest date and so on.
upvoted 1 times
...
...
vermeilyn
5 years, 1 month ago
1. Shouldn't do PARTITION BY CityID bc you are not ranking each date by city 2. Should do DENSE_RANK
upvoted 2 times
...
Robintang0924
5 years, 5 months ago
After 2nd thought, Bartek is right and rank/dense_rank is required AFTER group by function, since X axis is defined as 'The X axis contains the position that the city occupies relative to other cities.' and it seems it returns rank number instead of city id. Tazul already given correct data window function rank SQL based on assumption that rank is required.
upvoted 1 times
...
Robintang0924
5 years, 5 months ago
With above being said, if we really want to achieve the same result by window rani function, then below SQL should work the same way like a groupby/max only query. with top_city_date as ( Select CityID, AccountOpenedDate,RANK() OVER (PARTITION BY CityID ORDER BY AccountOpenedDate desc) as DateRank from Application.Cities join Sales.Customers ON CityID=PostalCityID) select CityID,AccountOpenedDate from top_city_date where DateRank=1 order by CityID;
upvoted 1 times
...
Robintang0924
5 years, 5 months ago
We don't need rank function, let's take a closer look at below requirements, a group by/max combination is good enough to get what we wanted data. ✑ The X axis contains the position that the city occupies relative to other cities. ✑ The Y axis contains the date that the first account in any city was opened. Furthermore, using data window function with group by clause could be very tricky and challenge especially when they are working on same group (cityID in our case), please check below SQL logic execution order, data window function was evaluated AFTER group by function(step 5-1 and 6) so it will NOT return what we wanted since data is already grouped/aggregrated by the same groupid/partition. 1. FROM 2. WHERE 3. GROUP BY 4. HAVING 5. SELECT 5-1. Evaluate Expressions 5-2. Remove Duplicates 6. ORDER BY 7. OFFSET-FETCH/TOP
upvoted 1 times
...
Abhilash_KK
5 years, 10 months ago
Rank function is irrelevant here, as we need only cityid and min(accountopendate) for the chart
upvoted 2 times
Bartek
5 years, 9 months ago
Not true. On the chart we have numbers like 1,2,3,4,5 despite of citynames. We have attribute the earlier date to number 5 and the newest date to number 1. In addition I think DENSE_RANK () II be better than RANK () cause RANK() is returning eg. x = 1 | RANK(x) = 1, x = 1 | RANK(x) = 1, x = 2 | RANK (x) = 3 WHEN DENSE_RANK() II return x = 1 | DENSE_RANK(x) = 1, x = 1 | DENSE_RANK(x) = 1, x = 2 | DENSE_RANK (x) = 2
upvoted 11 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 ...