Beginning SQL
Beginning SQL Beginning SQL
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
- 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 592: Chapter 9 SalesPersonId FirstName L
- Page 596: Chapter 9 278 SalesPersonId MemberI
- Page 600: Chapter 9 280 SalesPersonId MemberI
- 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
- Page 644: Chapter 11 The information that fol
- Page 648: Chapter 11 This SQL adds formats (d
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