Beginning SQL
Beginning SQL Beginning SQL
Chapter 9 274 DVDPrice column is in the Films table, so you need to find a link between the currently included tables in the FROM clause, which are the SalesPerson, Orders, OrderItems, and Films tables. That link is provided by the OrderItems table, which has the FilmId of the film purchased. Begin by adding the Films table and linking it with the current tables using an inner join: SELECT FirstName, LastName, MONTH(OrderDate)AS Month, YEAR(OrderDate) AS Year 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 FirstName,LastName,MONTH(OrderDate), YEAR(OrderDate); Finally, you can add the total sales per month to the SELECT clause: SELECT 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 FirstName,LastName,MONTH(OrderDate), YEAR(OrderDate); Remember that the SQL groups orders placed by salesperson and by month, so the SUM(DVDPrice) is the sum of all the DVDs sold by salesperson and by month. The results are as follows: FirstName LastName Month Year Total Daphne Moon 1 2007 80.94 Daphne Moon 11 2006 141.85 Daphne Moon 12 2006 221.74 Frasier Crane 10 2006 151.88 Frasier Crane 11 2006 12.99 Frasier Crane 12 2006 50.90 Sandra Hugson 1 2007 31.97 Sandra Hugson 7 2006 12.98 Sandra Hugson 8 2006 27.93 Sandra Hugson 9 2006 21.93 Sandra Hugson 10 2006 44.96 Sandra Hugson 11 2006 37.92 Sandra Hugson 12 2006 92.89 The query is nearly complete, save two things. First, what if two or more salespeople have the same name? This would mean that all their results would be lumped together because you’ve grouped by
- 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 590: FirstName and LastName. You need to
- 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
Chapter 9<br />
274<br />
DVDPrice column is in the Films table, so you need to find a link between the currently included tables<br />
in the FROM clause, which are the SalesPerson, Orders, OrderItems, and Films tables. That link is provided<br />
by the OrderItems table, which has the FilmId of the film purchased. Begin by adding the Films<br />
table and linking it with the current tables using an inner join:<br />
SELECT FirstName, LastName, MONTH(OrderDate)AS Month, YEAR(OrderDate) AS Year<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 FirstName,LastName,MONTH(OrderDate), YEAR(OrderDate);<br />
Finally, you can add the total sales per month to the SELECT clause:<br />
SELECT FirstName, LastName, MONTH(OrderDate)AS Month, YEAR(OrderDate) AS Year,<br />
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 FirstName,LastName,MONTH(OrderDate), YEAR(OrderDate);<br />
Remember that the <strong>SQL</strong> groups orders placed by salesperson and by month, so the SUM(DVDPrice) is<br />
the sum of all the DVDs sold by salesperson and by month. The results are as follows:<br />
FirstName LastName Month Year Total<br />
Daphne Moon 1 2007 80.94<br />
Daphne Moon 11 2006 141.85<br />
Daphne Moon 12 2006 221.74<br />
Frasier Crane 10 2006 151.88<br />
Frasier Crane 11 2006 12.99<br />
Frasier Crane 12 2006 50.90<br />
Sandra Hugson 1 2007 31.97<br />
Sandra Hugson 7 2006 12.98<br />
Sandra Hugson 8 2006 27.93<br />
Sandra Hugson 9 2006 21.93<br />
Sandra Hugson 10 2006 44.96<br />
Sandra Hugson 11 2006 37.92<br />
Sandra Hugson 12 2006 92.89<br />
The query is nearly complete, save two things. First, what if two or more salespeople have the same<br />
name? This would mean that all their results would be lumped together because you’ve grouped by