Beginning SQL

Beginning SQL Beginning SQL

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

GROUP BY SalesPerson.SalesPersonId, SalesPerson.FirstName, SalesPerson.LastName, MemberDetails.MemberId, MemberDetails.FirstName, MemberDetails.LastName The FROM clause has two INNER JOIN statements added to it, joining the MemberDetails and SalesPerson tables: FROM (Orders INNER JOIN MemberDetails ON Orders.MemberId = MemberDetails.MemberId) INNER JOIN SalesPerson ON Orders.SalesPersonId = SalesPerson.SalesPersonId Finally, you updated the ORDER BY clause so that it orders by the SalesPerson table’s SalesPersonId column, because this column appears in the GROUP BY clause, and only columns in GROUP BY can be ordered. ORDER BY SalesPerson.SalesPersonId The final results are shown in the following table: SalesPersonId FirstName LastName MemberId FirstName LastName 1 Sandra Hugson 4 Steve Gee 1 Sandra Hugson 6 Jenny Jones 1 Sandra Hugson 7 John Jackson 1 Sandra Hugson 9 Seymour Botts 2 Frasier Crane 4 Steve Gee 2 Frasier Crane 7 John Jackson 2 Frasier Crane 10 Susie Simons That completes the first part of the Try It Out — on to the second part. The second step asked you to find out how many repeat orders each customer placed with each salesperson. It’s fairly easy to modify the SQL from the first step so that in addition to the current results it also shows the number of total repeat orders in each group. Remember that each group consists of a SalesPerson and the orders by the same Customer. However, the question asks for the number of repeat orders each customer made, not the total number of orders. The first order made doesn’t count, so you need to add COUNT(*) but then deduct 1 from it: SELECT SalesPerson.SalesPersonId, SalesPerson.FirstName, SalesPerson.LastName, MemberDetails.MemberId, Advanced Queries 281

GROUP BY SalesPerson.SalesPersonId,<br />

SalesPerson.FirstName,<br />

SalesPerson.LastName,<br />

MemberDetails.MemberId,<br />

MemberDetails.FirstName,<br />

MemberDetails.LastName<br />

The FROM clause has two INNER JOIN statements added to it, joining the MemberDetails and<br />

SalesPerson tables:<br />

FROM (Orders INNER JOIN MemberDetails<br />

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

INNER JOIN SalesPerson<br />

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

Finally, you updated the ORDER BY clause so that it orders by the SalesPerson table’s SalesPersonId column,<br />

because this column appears in the GROUP BY clause, and only columns in GROUP BY can be ordered.<br />

ORDER BY SalesPerson.SalesPersonId<br />

The final results are shown in the following table:<br />

SalesPersonId FirstName LastName MemberId FirstName LastName<br />

1 Sandra Hugson 4 Steve Gee<br />

1 Sandra Hugson 6 Jenny Jones<br />

1 Sandra Hugson 7 John Jackson<br />

1 Sandra Hugson 9 Seymour Botts<br />

2 Frasier Crane 4 Steve Gee<br />

2 Frasier Crane 7 John Jackson<br />

2 Frasier Crane 10 Susie Simons<br />

That completes the first part of the Try It Out — on to the second part.<br />

The second step asked you to find out how many repeat orders each customer placed with each<br />

salesperson.<br />

It’s fairly easy to modify the <strong>SQL</strong> from the first step so that in addition to the current results it also<br />

shows the number of total repeat orders in each group. Remember that each group consists of a<br />

SalesPerson and the orders by the same Customer. However, the question asks for the number of repeat<br />

orders each customer made, not the total number of orders. The first order made doesn’t count, so you<br />

need to add COUNT(*) but then deduct 1 from it:<br />

SELECT SalesPerson.SalesPersonId,<br />

SalesPerson.FirstName,<br />

SalesPerson.LastName,<br />

MemberDetails.MemberId,<br />

Advanced Queries<br />

281

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

Saved successfully!

Ooh no, something went wrong!