Beginning SQL

Beginning SQL Beginning SQL

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

2. The film club chairperson also wants to know how many repeat orders each customer placed with the salesperson. The SQL to do this is shown below: SELECT SalesPerson.SalesPersonId, SalesPerson.FirstName, SalesPerson.LastName, MemberDetails.MemberId, MemberDetails.FirstName, MemberDetails.LastName, COUNT(*) - 1 FROM (Orders INNER JOIN MemberDetails ON Orders.MemberId = MemberDetails.MemberId) INNER JOIN SalesPerson ON Orders.SalesPersonId = SalesPerson.SalesPersonId GROUP BY SalesPerson.SalesPersonId, SalesPerson.FirstName, SalesPerson.LastName, MemberDetails.MemberId, MemberDetails.FirstName, MemberDetails.LastName HAVING COUNT(Orders.MemberId) > 1 ORDER BY SalesPerson.SalesPersonId; How It Works First, you need to think about what the question involves. Basically, it involves counting how many times a customer has ordered from the same salesperson. If the customer makes repeat orders from the same salesperson, then display that record in the results. Now you need to work out where the data to answer the question comes from. Details about orders are found in the Orders table. Start simple and create a SELECT list that obtains the SalesPersonId and MemberId — the customer who placed the order: SELECT SalesPersonId, MemberId FROM Orders; This basic query gives the following results: SalesPersonId MemberId 2 7 2 4 2 9 2 7 2 7 2 10 2 4 2 4 Advanced Queries Table continued on following page 277

2. The film club chairperson also wants to know how many repeat orders each customer placed<br />

with the salesperson. The <strong>SQL</strong> to do this is shown below:<br />

SELECT SalesPerson.SalesPersonId,<br />

SalesPerson.FirstName,<br />

SalesPerson.LastName,<br />

MemberDetails.MemberId,<br />

MemberDetails.FirstName,<br />

MemberDetails.LastName,<br />

COUNT(*) - 1<br />

FROM (Orders INNER JOIN MemberDetails<br />

ON Orders.MemberId = MemberDetails.MemberId)<br />

INNER JOIN SalesPerson<br />

ON Orders.SalesPersonId = SalesPerson.SalesPersonId<br />

GROUP BY SalesPerson.SalesPersonId,<br />

SalesPerson.FirstName,<br />

SalesPerson.LastName,<br />

MemberDetails.MemberId,<br />

MemberDetails.FirstName,<br />

MemberDetails.LastName<br />

HAVING COUNT(Orders.MemberId) > 1<br />

ORDER BY SalesPerson.SalesPersonId;<br />

How It Works<br />

First, you need to think about what the question involves. Basically, it involves counting how many<br />

times a customer has ordered from the same salesperson. If the customer makes repeat orders from the<br />

same salesperson, then display that record in the results.<br />

Now you need to work out where the data to answer the question comes from. Details about orders are<br />

found in the Orders table. Start simple and create a SELECT list that obtains the SalesPersonId and<br />

MemberId — the customer who placed the order:<br />

SELECT SalesPersonId, MemberId<br />

FROM Orders;<br />

This basic query gives the following results:<br />

SalesPersonId MemberId<br />

2 7<br />

2 4<br />

2 9<br />

2 7<br />

2 7<br />

2 10<br />

2 4<br />

2 4<br />

Advanced Queries<br />

Table continued on following page<br />

277

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

Saved successfully!

Ooh no, something went wrong!