Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!