PrepAway - Latest Free Exam Questions & Answers

Which Transact-SQL query should you use?

You support a database structure shown in the exhibit. (Refer to the Exhibit.)

You need to write a query that displays the following details:
Total sales made by sales people, year, city, and country
Sub totals only at the city level and country level
A grand total of the sales amount
Which Transact-SQL query should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
SELECT SalesPerson.Name, Country, City,
DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total
FROM Sale INNER JOIN SalesPerson
ON Sale.SalesPersonID = SalesPerson.SalesPersonID
GROUP BY GROUPING SETS((SalesPerson.Name, Country, City, DatePart(yyyy,
SaleDate)), (Country, City), (Country), ())

B.
SELECT SalesPerson.Name, Country, City,
DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total
FROM Sale INNER JOIN SalesPerson
ON Sale.SalesPersonID = SalesPerson.SalesPersonID
GROUP BY CUBE(SalesPerson.Name, Country, City, DatePart(yyyy, SaleDate))

C.
SELECT SalesPerson.Name, Country, City,
DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total
FROM Sale INNER JOIN SalesPerson
ON Sale.SalesPersonID = SalesPerson.SalesPersonID
GROUP BY CUBE(SalesPerson.Name, DatePart(yyyy, SaleDate), City, Country)

D.
SELECT SalesPerson.Name, Country, City,
DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total
FROM Sale INNER JOIN SalesPerson
ON Sale.SalesPersonID = SalesPerson.SalesPersonID
GROUP BY ROLLUP(SalesPerson.Name, DatePart(yyyy, SaleDate), City, Country)

Explanation:
Verified answer as correct.
Reference: http://www.grapefruitmoon.net/diving-into-t-sql-grouping-sets/
Reference: http://msdn.microsoft.com/en-us/library/ms177673.aspx

9 Comments on “Which Transact-SQL query should you use?

  1. yeeen says:

    I just took the exam today! OMG the qns are so similar! From the score it seems that I have only 1 wrong and this is the qn!

    All the choices are the same (in different order) except A and B, even the qn is the same. The choices I mentioned below are pertaining to the qn above with the mentioned modification (pls ignore the different order in the exam).

    For A it is “GROUP BY GROUPING SETS((SalesPerson.Name, Country, City, DatePart(yyyy,
    SaleDate)), ())” instead of “GROUP BY GROUPING SETS((SalesPerson.Name, Country, City, DatePart(yyyy, SaleDate)), (Country, City), (Country), ())”! For B it is “ROLLUP” instead of “CUBE”.

    It seems that all choices are impossible, so I chose C (the only one with CUBE). As I wanted to show the subtotals for city and country, I have to choose CUBE. But the qn said “Sub totals ONLY at the city level and country level”, so I actually pondered for a very long time before I chose C but I am still not confident. Can anyone tell me what should I choose instead and why am I wrong?

    I suspect there is a typo in the choices. Can someone comment??




    0



    0
    1. riemi says:

      I think answer A (grouping sets) is correct.

      Did try it in AdventureWorks2012 database:

      SELECT T.[Group] AS N’Region’, T.CountryRegionCode AS N’Country’
      ,S.Name AS N’Store’, H.SalesPersonID
      ,SUM(TotalDue) AS N’Total Sales’
      FROM Sales.Customer C
      INNER JOIN Sales.Store AS S
      ON C.StoreID = S.BusinessEntityId
      INNER JOIN Sales.SalesTerritory AS T
      ON C.TerritoryID = T.TerritoryID
      INNER JOIN Sales.SalesOrderHeader AS H
      ON C.CustomerID = H.CustomerID
      WHERE T.[Group] = N’Europe’
      AND T.CountryRegionCode IN(N’DE’, N’FR’)
      AND H.SalesPersonID IN(287, 288, 290)
      AND SUBSTRING(S.Name,1,4)IN(N’Vers’, N’Spa ‘)
      GROUP BY GROUPING SETS
      ((T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID), (T.[Group], T.CountryRegionCode), (T.CountryRegionCode), ())




      0



      0
  2. salim says:

    answer A is correct, i tried by creating same tables in my db,

    SELECT SalesPerson.Name, Country, City,
    DatePart(yyyy, SaleDate) AS Year, Sum(Amount) AS Total
    FROM Sale INNER JOIN SalesPerson
    ON Sale.SalesPersonID = SalesPerson.SalesPersonID
    GROUP BY GROUPING SETS((SalesPerson.Name, Country, City, DatePart(yyyy,
    SaleDate)), (Country, City), (Country), ())

    –grouping sets.




    0



    0
  3. Ale says:

    Agree with A
    CUBE returns every possible combination of the elements in the GROUP BY.

    SalesPerson.Name, Country, City, DatePart(yyyy, SaleDate)
    SalesPerson.Name, Country, City
    SalesPerson.Name, Country
    SalesPerson.Name
    Country, City, DatePart(yyyy, SaleDate)
    Country, City
    Country

    etc




    0



    0

Leave a Reply