PrepAway - Latest Free Exam Questions & Answers

How should you complete the Transact-SQL statement?

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:

PrepAway - Latest Free Exam Questions & Answers

Answer:

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

6 Comments on “How should you complete the Transact-SQL statement?

  1. LW says:

    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




    14



    0
  2. Peter says:

    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




    19



    6
  3. eder says:

    SELECT CityID,min(AccountOpenedDate),
    DENSE_RANK() OVER(
    ORDER BY MIN(AccountOpenedDate)DESC )
    from application.cities
    INNER JOIN SalesCustomers
    ON CityID=PostalCityID
    group by CityID
    ORDER BY MIN(AccountOpenedDate)DESC

    or

    SELECT CityID,min(AccountOpenedDate),
    RANK() OVER(
    ORDER BY MIN(AccountOpenedDate)DESC )
    from application.cities
    INNER JOIN SalesCustomers
    ON CityID=PostalCityID
    group by CityID
    ORDER BY MIN(AccountOpenedDate)DESC

    DENSE RANK
    Esta función devuelve el rango de cada fila dentro de una partición de conjunto de resultados, sin espacios en los valores de clasificación.
    El rango de una fila específica es uno más el número de valores de rango distintos que vienen antes de esa fila específica.

    RANK
    Devuelve el rango de cada fila dentro de la partición de un conjunto de resultados.
    El rango de una fila es uno más el número de filas que vienen antes de la fila en cuestión.




    2



    0
  4. eder says:

    THE CORRECT ANSWER IS :

    TIENEN QUE OBSERVAR LA GRAFICA EN LA COORDENADA Y
    SELECT CityID,min(AccountOpenedDate),
    DENSE_RANK() OVER(
    ORDER BY MIN(AccountOpenedDate)DESC )
    from application.cities
    INNER JOIN SalesCustomers
    ON CityID=PostalCityID
    group by CityID
    ORDER BY MIN(AccountOpenedDate)DESC




    8



    0

Leave a Reply