Sie sind auf Seite 1von 4

Name: Shantell Brown

Date: 2/4/2015

Exercise Instructions
1
2
3
4

Type your name and the date into the space provided.
Use the SQL Server Management Studio to complete this lab.
Write T-SQL statements to query the tables contained in the IST272EagleCorp
database. Paste your solutions to exercises 1 through 10 into the indicated
areas below.
Upload and submit before the due date.

1. Write a SELECT statement that returns two columns from the CustOrderLine table: PartNumber and
NumOfPartsOrdered, where NumOfPartsOrdered is the sum of the OrderQuantity column. Group
the result set by PartNumber.
Paste exercise 1 answer here:
SELECT PartNumber, SUM(OrderQuantity) AS NumOfPartsOrdered
FROM CustOrderLine
GROUP BY PartNumber

------------------------------------------------

2. Write a SELECT statement that returns the CustomerID and NumOfPartsOrdered, where
NumOfPartsOrdered is the total number of parts ordered by the customer (sum the OrderQuantity).
Group the result set by CustomerID. Return only 5 rows, corresponding to the 5 customers who've
ordered the most parts.
Hint: Use the TOP clause and join CustOrder to CustOrderLine.
Paste exercise 2 answer here:
SELECT TOP 5 CustomerID, SUM(OrderQuantity) AS NumOfPartsOrdered
FROM CustOrder JOIN CustOrderLine
ON CustOrder.OrderID = CustOrderLine.OrderID
GROUP BY CustomerID
ORDER BY NumOfPartsOrdered DESC

------------------------------------------------

3. Write a SELECT statement that returns three columns consisting of the CustomerID, the total number
of orders the customer has placed, and the undiscounted
total dollar value of the orders the customer has placed. Name these columns:
CustomerID
NumberOfOrders
TotalDollarValueOfOrders
Sort the result set so the Customer with the least orders appears first.

hints:
Join the CustOrderLine table to the CustOrder table. Use COUNT(DISTINCT CustOrderLine.OrderID)
and SUM(UnitPrice * OrderQuantity).
Paste exercise 3 answer here:
SELECT CustomerID, COUNT(DISTINCT CustOrder.OrderID) AS NumOfOrders, SUM(UnitPrice *
OrderQuantity) AS TotalDollarValueOfOrders
FROM CustOrder JOIN CustOrderLine on CustOrder.OrderID = CustorderLine.OrderID
GROUP BY CustomerID, OrderQuantity
ORDER BY NumOfOrders ASC;

------------------------------------------------

4. Modify the solution to exercise 3 to filter for CustomerIDs that begin with C (only include rows that
begin with C)
Hint: add a where clause
Paste exercise 4 answer here:
SELECT CustomerID, COUNT(distinct CustOrder.OrderID) AS NumOfOrders, SUM(UnitPrice *
OrderQuantity) AS TotalDollarValueOfOrders
FROM CustOrder JOIN CustOrderLine ON CustOrder.OrderID = CustorderLine.OrderID
WHERE CustomerID LIKE 'C%'
GROUP BY CustomerID, OrderQuantity
ORDER BY NumOfOrders ASC

------------------------------------------------

5. Write a SELECT statement that returns three columns consisting of the CustomerID, the total number
of orders the customer has placed, and the average undiscounted dollar value of the orders the
customer has placed. Name these columns:
CustomerID
NumberOfOrders
AvgDollarValueOfOrders
Sort the result set so the Customer with the least orders appears first.
hints:
1. Join the CustOrderLine table to the CustOrder table. Make sure to count Orders not orderline items.
The following example does NOT produce the correct answer for exercise 5.
SELECT CustomerID, COUNT(CO.OrderID)As NumberofLineItems,
AVG(UnitPrice * OrderQuantity) AS AVGDollarValueOfLineItems
FROM CustOrderLine AS COL Join CustOrder AS CO
ON COL.OrderID = CO.OrderID
GROUP BY CustomerID
ORDER BY NumberofOrders DESC

2. The above query can be changed to count Orders by replacing the COUNT(Co.OrderID) with
COUNT(Distinct Co.OrderID).
3. The above query can be changed to calculate AvgDollarValueOfOrders by replacing the
AVG(UnitPrice * OrderQuantity) with SUM(UnitPrice * OrderQuantity)/COUNT(DISTINCT
CustOrderLine.OrderID). The problem with using AVG as shown above for problem five is that it
bases its calculation on the number of lineitems not the number of orders.
Paste exercise 5 answer here:
SELECT CustomerID, COUNT(DISTINCT COL.OrderID)As NumberOfOrders,
SUM(UnitPrice * OrderQuantity)/COUNT(DISTINCT COL.OrderID) AS AVGDollarValueOfLineItems
FROM CustOrder AS CO JOIN CustOrderLine AS COL
ON CO.OrderID = COL.OrderID
GROUP BY CustomerID
ORDER BY NumberOfOrders ASC

------------------------------------------------

6. Modify the solution to exercise 5 to only include groups in the final result set that have a
NumberOfOrders value of 5 or greater.
Hint: add a HAVING clause
Paste exercise 6 answer here:
SELECT CustomerID, COUNT(DISTINCT COL.OrderID)As NumberOfOrders,
SUM(UnitPrice * OrderQuantity)/COUNT(DISTINCT COL.OrderID) AS AVGDollarValueOfLineItems
FROM CustOrder AS CO Join CustOrderLine AS COL
ON CO.OrderID = COL.OrderID
GROUP BY CustomerID, OrderQuantity
HAVING COUNT(DISTINCT COL.OrderID) >= 5
ORDER BY NumberOfOrders ASC

------------------------------------------------

7. Write a SELECT statement that returns two columns from the PackingSlip table: EmployeeID and
PackagesPacked, where PackagesPacked is a count of the rows on the packing slip table associated
with the EmployeeID. Sort the result set by EmployeeID in Descending sequence.
Paste exercise 7 answer here:
Select EmployeeID, Count(*) AS PackagesPacked
From PackingSlip
Group By EmployeeID
Order By EmployeeID DESC

------------------------------------------------

8. Modify the solution to exercise 7 to only include groups in the final result set that have a
PackagesPacked value of 120 or greater.
Paste exercise 8 answer here:

Select EmployeeID, Count(*) AS PackagesPacked


From PackingSlip
Group By EmployeeID
Having Count(PackageNumber) >= 120
Order By EmployeeID DESC

------------------------------------------------

9. Modify the solution to exercise 7 to filter to only include rows from the source table that have a
ShippedDate that falls in the following range:
'2008-07-01' to '2008-07-31'

Paste exercise 9 answer here:


Select EmployeeID, Count(*) AS PackagesPacked
From PackingSlip
Where ShippedDate Between '2008-07-01' AND '2008-07-31'
Group By EmployeeID
Order By EmployeeID DESC

------------------------------------------------

10. Modify solution to exercise 9 to include a row that gives the total number of packages packed in July
2008.
Hint: make use of WITH ROLLUP
Paste exercise 10 answer here:
Select EmployeeID, Count(*) AS PackagesPacked
From PackingSlip
Where ShippedDate Between '2008-07-01' AND '2008-07-31'
Group By EmployeeID WITH ROLLUP
Order By EmployeeID DESC

------------------------------------------------

Das könnte Ihnen auch gefallen