DB
DB
Ch7 - Ch7
27. Write a query to count the number of customers with a customer balance over $500.
SELECT COUNT(*)
FROM CUSTOMER
WHERE CUS_BALANCE >500;
28. Generate a listing of all purchases made by the customers, using the output shown in Figure
P7.28 as your guide. (Hint: Use the ORDER BY clause to order the resulting rows as shown
in Figure P7.28)
29. Using the output shown in Figure P7.29 as your guide, generate the listing of customer
purchases, including the subtotals for each of the invoice line numbers. (Hint: Modify the
query format used to produce the listing of customer purchases in Problem 18, delete the
INV_DATE column, and add the derived (computed) attribute LINE_UNITS *
LINE_PRICE to calculate the subtotals.)
30. Modify the query used in Problem 29 to produce the summary shown in Figure P7.30.
31. Modify the query in Problem 30 to include the number of individual product purchases made
by each customer. (In other words, if the customer’s invoice is based on three products, one
per LINE_NUMBER, you would count three product purchases. If you examine the original
invoice data, you will note that customer 10011 generated three invoices, which contained a
total of six lines, each representing a product purchase.) Your output values must match
those shown in Figure P7.31.
32. Use a query to compute the average purchase amount per product made by each customer.
(Hint: Use the results of Problem 31 as the basis for this query.) Your output values must
match those shown in Figure P7.32. Note that the Average Purchase Amount is equal to the
Total Purchases divided by the Number of Purchases.
33. Create a query to produce the total purchase per invoice, generating the results shown in
Figure P7.33. The Invoice Total is the sum of the product purchases in the LINE that
corresponds to the INVOICE.
SELECT LINE.INV_NUMBER,
Sum(LINE.LINE_UNITS*LINE.LINE_PRICE) AS 'Invoice Total'
FROM LINE
GROUP BY LINE.INV_NUMBER;
34. Use a query to show the invoices and invoice totals as shown in Figure P7.34. (Hint: Group
by the CUS_CODE.)
35. Write a query to produce the number of invoices and the total purchase amounts by
customer, using the output shown in Figure P7.35 as your guide. (Compare this summary to
the results shown in Problem 34.)
You can use any aggregate function such as Average, Max, Min, Sum etc. as shown below:
SELECT CUS_CODE,
COUNT(INV_NUMBER) AS 'Number of Invoices',
AVG(INV_TOT) AS 'Average Invoice Amount',
MAX(INV_TOT) AS 'Max Invoice Amount',
MIN(INV_TOT) AS 'Min Invoice Amount',
Sum(INV_TOT) AS 'Total Customer Purchases'
FROM (SELECT CUS_CODE, L.INV_NUMBER AS INV_NUMBER,
Sum(L.LINE_UNITS*L.LINE_PRICE) AS INV_TOT
FROM INVOICE I, LINE L
WHERE I.INV_NUMBER = L.INV_NUMBER
GROUP BY CUS_CODE, L.INV_NUMBER) AS IL
GROUP BY CUS_CODE;
36. Using the query results in Problem 35 as your basis, write a query to generate the total
number of invoices, the invoice total for all of the invoices, the smallest of the customer
purchase amounts, the largest of the customer purchase amounts, and the average of all
of the customer purchase amounts. (Hint: Check the figure output in Problem 35.) Your
output must match Figure P7.36.
37. List the balance characteristics of the customers who have made purchases during the
current invoice cycle—that is, for the customers who appear in the INVOICE table. The
results of this query are shown in Figure P7.37.
or
38. Using the results of the query created in Problem 37, provide a summary of customer balance
characteristics as shown in Figure P7.38.
or
39. Create a query to find the customer balance characteristics for all customers, including the
total of the outstanding balances. The results of this query are shown in Figure P7.39.
40. Find the listing of customers who did not make purchases during the invoicing period.
Your output must match the output shown in Figure P7.40.
FIGURE P7.40 Customer Balances for Customers Who Did Not Make
Purchases
41. Find the customer balance summary for all customers who have not made purchases during
the current invoicing period. The results are shown in Figure P7.41.
FIGURE P7.41 Summary of Customer Balances for Customers Who Did Not
Make Purchases
or
42. Create a query to produce the summary of the value of products currently in inventory. Note
that the value of each product is produced by the multiplication of the units currently in
inventory and the unit price. Use the ORDER BY clause to match the order shown in Figure
P7.42.
43. Using the results of the query created in Problem 42, find the total value of the product
inventory. The results are shown in Figure P7.43.