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:
Explanation:
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
https://msdn.microsoft.com/en-us/library/ms176102.aspx
I disagree with the Partition By – this will restart the rank within each City ID which we don’t want since all City IDs will be ranked 1. We want the ranking across all cities, so no Partition By, only an Order By
Correct statement is
SELECT
CityID,
MIN(AccountOpenedDate),
RANK() OVER
(ORDER BY min(AccountOpenedDate) DESC)
FROM Cities
INNER JOIN Customers ON CityID = PostalCityID
GROUP BY CityID
ORDER BY MIN(AccountOpenedDate) DESC
agree