Beginning SQL
Beginning SQL Beginning SQL
FirstName and LastName. You need to add SalesPersonId, which is unique to each salesperson, to the GROUP BY clause, as well as display it in the final results: SELECT SalesPerson.SalesPersonId, FirstName, LastName, MONTH(OrderDate)AS Month, YEAR(OrderDate) AS Year, SUM(DVDPrice) AS Total FROM ((Orders INNER JOIN SalesPerson ON Orders.SalesPersonId = SalesPerson.SalesPersonId) INNER JOIN OrderItems ON OrderItems.OrderId = Orders.OrderId) INNER JOIN Films ON Films.FilmId = OrderItems.FilmId GROUP BY SalesPerson.SalesPersonId, FirstName,LastName,MONTH(OrderDate), YEAR(OrderDate); Executing the query provides the following results: SalesPersonId FirstName LastName Month Year Total 1 Sandra Hugson 1 2007 31.97 1 Sandra Hugson 7 2006 12.98 1 Sandra Hugson 8 2006 27.93 1 Sandra Hugson 9 2006 21.93 1 Sandra Hugson 10 2006 44.96 1 Sandra Hugson 11 2006 37.92 1 Sandra Hugson 12 2006 92.89 2 Frasier Crane 10 2006 151.88 2 Frasier Crane 11 2006 12.99 2 Frasier Crane 12 2006 50.90 3 Daphne Moon 1 2007 80.94 3 Daphne Moon 11 2006 141.85 3 Daphne Moon 12 2006 221.74 The second slight issue is the order of the results. They would be easier to read if they were ordered by salesperson and then by month and year, as shown in the ORDER BY clause of the following statement: SELECT SalesPerson.SalesPersonId, FirstName, LastName, MONTH(OrderDate)AS Month, YEAR(OrderDate) AS Year, SUM(DVDPrice) AS Total FROM ((Orders INNER JOIN SalesPerson ON Orders.SalesPersonId = SalesPerson.SalesPersonId) INNER JOIN OrderItems ON OrderItems.OrderId = Orders.OrderId) INNER JOIN Films ON Films.FilmId = OrderItems.FilmId GROUP BY SalesPerson.SalesPersonId, FirstName,LastName,MONTH(OrderDate), YEAR(OrderDate) ORDER BY LastName, FirstName, YEAR(OrderDate), MONTH(OrderDate); The final results are as follows: Advanced Queries 275
- Page 538: MemberId 1 10 14 6 5 8 7 11 15 4 13
- Page 542: Now NOT EXISTS returns true, and as
- Page 546: This is the same as the query just
- Page 550: clause specifying that the film’s
- Page 554: 4. Next, quickly double-check a few
- Page 558: Now that you’ve confirmed that th
- Page 562: Summary Queries within Queries This
- Page 568: Chapter 9 264 Figure 9-1 The SQL ne
- Page 572: Chapter 9 266 INSERT INTO OrderItem
- Page 576: Chapter 9 268 VALUES (5,7, 2,’200
- Page 580: Chapter 9 Now that you’ve added t
- Page 584: Chapter 9 272 FirstName LastName Mo
- Page 588: Chapter 9 274 DVDPrice column is in
- Page 594: 2. The film club chairperson also w
- Page 598: Executing the preceding query provi
- Page 602: GROUP BY SalesPerson.SalesPersonId,
- Page 606: Executing the preceding SQL creates
- Page 610: ❑ Use IN with your subqueries. Wh
- Page 614: 10 Views Views, as the name implies
- Page 618: Try It Out Building a View 1. The p
- Page 622: FROM dbo.MemberDetails INNER JOIN d
- Page 626: For example, you might want a list
- Page 630: However, if you take the case of Me
- Page 634: However, someone writes a query tha
- Page 638: Summary As you learned, views are l
FirstName and LastName. You need to add SalesPersonId, which is unique to each salesperson, to the<br />
GROUP BY clause, as well as display it in the final results:<br />
SELECT SalesPerson.SalesPersonId, FirstName, LastName, MONTH(OrderDate)AS Month,<br />
YEAR(OrderDate) AS Year, SUM(DVDPrice) AS Total<br />
FROM ((Orders INNER JOIN SalesPerson ON Orders.SalesPersonId =<br />
SalesPerson.SalesPersonId)<br />
INNER JOIN OrderItems ON OrderItems.OrderId = Orders.OrderId)<br />
INNER JOIN Films ON Films.FilmId = OrderItems.FilmId<br />
GROUP BY SalesPerson.SalesPersonId, FirstName,LastName,MONTH(OrderDate),<br />
YEAR(OrderDate);<br />
Executing the query provides the following results:<br />
SalesPersonId FirstName LastName Month Year Total<br />
1 Sandra Hugson 1 2007 31.97<br />
1 Sandra Hugson 7 2006 12.98<br />
1 Sandra Hugson 8 2006 27.93<br />
1 Sandra Hugson 9 2006 21.93<br />
1 Sandra Hugson 10 2006 44.96<br />
1 Sandra Hugson 11 2006 37.92<br />
1 Sandra Hugson 12 2006 92.89<br />
2 Frasier Crane 10 2006 151.88<br />
2 Frasier Crane 11 2006 12.99<br />
2 Frasier Crane 12 2006 50.90<br />
3 Daphne Moon 1 2007 80.94<br />
3 Daphne Moon 11 2006 141.85<br />
3 Daphne Moon 12 2006 221.74<br />
The second slight issue is the order of the results. They would be easier to read if they were ordered by<br />
salesperson and then by month and year, as shown in the ORDER BY clause of the following statement:<br />
SELECT SalesPerson.SalesPersonId, FirstName, LastName, MONTH(OrderDate)AS Month,<br />
YEAR(OrderDate) AS Year, SUM(DVDPrice) AS Total<br />
FROM ((Orders INNER JOIN SalesPerson ON Orders.SalesPersonId =<br />
SalesPerson.SalesPersonId)<br />
INNER JOIN OrderItems ON OrderItems.OrderId = Orders.OrderId)<br />
INNER JOIN Films ON Films.FilmId = OrderItems.FilmId<br />
GROUP BY SalesPerson.SalesPersonId, FirstName,LastName,MONTH(OrderDate),<br />
YEAR(OrderDate)<br />
ORDER BY LastName, FirstName, YEAR(OrderDate), MONTH(OrderDate);<br />
The final results are as follows:<br />
Advanced Queries<br />
275