exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 70 discussion

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

SIMULATION -
You create a table named Products.Sales by running the following Transact-SQL statement:

You add the following data to the table.

You are developing a report to display monthly sales data.
You need to create a Transact-SQL query to meet the following requirements:
✑ Retrieve a column for the year followed by a column for each month from January through December.
✑ Include the total sales amount for each month.
Aggregate columns by year, month, and then amount.

Construct the query using the following guidelines:
✑ Use the MONTH keyword as the interval when using the DATANAME function.
✑ Do not modify the provided IN clause.
✑ Do not surround object names with square brackets.
✑ Do not use implicit joins.
✑ Do not use the DATEPART function.
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.
1. SELECT * FROM
2. (SELECT YEAR(SalesData)) AS Year, DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount
3.
4. ) AS MonthlySalesData
5.
6. FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December))

AS MonthNamePivot -

Show Suggested Answer Hide Answer
Suggested Answer: Please see explanation
1 SELECT * FROM
2 (SELECT YEAR(SalesData)) AS Year, DATENAME (MONTH, SalesDate) AS Month, SUM(SalesAmount) AS Amount
3 FROM Products.Sales GROUP BY Year, Month
4 ) AS MonthlySalesData
5 PIVOT SUM(Amount)
6 FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December))

AS MonthNamePivot -
Note:
Line 2: Add SUM( ) around SalesAmount
Line 3: Add: FROM Products.Sales GROUP BY Year, Month
Line 5: Add: PIVOT SUM(Amount)

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
Tazul
Highly Voted 5 years, 7 months ago
SELECT * FROM (SELECT YEAR(SalesDate) AS Year, DATENAME(MONTH, SalesDate) AS Month, SalesAmount AS Amount from Products.sales ) AS MonthlySalesData PIVOT (Sum(Amount) FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December)) AS MonthNamePivot
upvoted 37 times
rjile
4 years, 10 months ago
Tazul number one
upvoted 2 times
...
Vermonster
4 years, 4 months ago
Tazul has it right
upvoted 1 times
...
...
Dieter
Highly Voted 5 years, 8 months ago
GROUP BY Year, Month => will not work since GROUP BY is executed before Select statement => syntax erro. better: GROUP BY Year(SalesDate), Month(SalesDate)
upvoted 8 times
AshleyLiang
5 years, 8 months ago
Good point.
upvoted 3 times
...
M4x
5 years, 8 months ago
Better use DATENAME same function of SELECT. GROUP BY YEAR(SalesData), DATENAME(MONTH, SalesDate)
upvoted 1 times
...
Andy7622
4 years, 4 months ago
Why do we need GROUP BY in derived table at all?
upvoted 1 times
...
...
eduardogtc
Most Recent 4 years, 7 months ago
I don't think we need the sum in the SUM(SalesAmount). The pivot query will sum anyway
upvoted 2 times
...
Barbedx
5 years, 2 months ago
Everyone forgive about Total? SELECT * FROM (SELECT YEAR(salesDate) AS Year, DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount From sales UNION ALL select 'Total',DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount From sales ) AS MonthlySalesData PIVOT ( SUM(Amount) FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December)) as MonthNamePivot
upvoted 1 times
xd1
5 years ago
you already calculate the total (sum) for each month using the pivottable. no need to additionally calculate this.
upvoted 1 times
...
...
moehijawe
5 years, 4 months ago
SELECT * FROM (SELECT YEAR(SalesData)) AS Year, DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount from Products.Sales) AS MonthlySalesData pivot (sum(Amount) FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December)) AS MonthNamePivot
upvoted 1 times
...
moehijawe
5 years, 4 months ago
SELECT * FROM (SELECT YEAR(SalesData)) AS Year, DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount FROM Products.Sales) AS MonthlySalesData PIVOT (SUM(Amount) FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December)) as AS PivotMonthlySalesData
upvoted 8 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