IS675 – Database Design
Bilal Gonen
SQL-4

#1. Create a report listing the shipping status of each
order line. Include the columns
shown on the sample result table below. The sample
below is not especially well formatted, but it is provided to give you a
general idea of the required information.
The shipping status is as follows:
Completed:
The quantity shipped equals the quantity ordered.
Over Shipped:
The quantity shipped is greater than the quantity ordered.
Partially Shipped: The quantity shipped is greater than zero,
but does not equal the quantity ordered.
Not Shipped: No items have been shipped.
Sort the output by itemID within orderID. I recommend using a SQL view to summarize the total quantity shipped from the shipline table so that the main query does not have to use an aggregate function. I recommend using the CASE statement to determine the shipping status.
#1. Code
CREATE VIEW
SELECT item.itemID,
item.description,
item.ListPrice,
h1.LastCost,
h1.LastCostDate
FROM TblItem item
LEFT JOIN tblitemCostHistory
h1
ON h1.itemID = item.itemID
WHERE h1.LastCostDate = (
SELECT
FROM tblitemCostHistory h2
WHERE h2.itemID = h1.itemID
)
CREATE VIEW
SELECT ol.orderID "OrderID",
ord.OrderDate,
cust.LastName + ',
'+ SUBSTRING(cust.firstname,1,1)+'.' "CustomerName",
ol.itemID "ItemID",
ol.quantity "QuantityOrdered",
ISNULL(SUM(sl.QtyShipped), 0) "TotalShipped",
ol.quantity - ISNULL(SUM(sl.QtyShipped), 0)
"quantityremaining"
FROM TblOrderLine ol
LEFT JOIN TblOrder ord
ON ord.orderID = ol.OrderID
LEFT JOIN TblShipLine sl
ON sl.ItemID = ol.ItemID AND
sl.OrderID = ol.OrderID
LEFT JOIN TblCustomer cust
ON ord.customerID = cust.customerID
GROUP BY ol.orderID,
cust.LastName,
cust.FirstName,
ol.ItemID,
ord.OrderDate,
ol.quantity
SELECT v.orderid "Order ID",
CONVERT(VARCHAR, v.OrderDate, 101) "Order Date",
v.CustomerName
"Customer Name",
v.ItemID "Item
ID",
item.description
"Item Name",
v.QuantityOrdered
"Quantity Ordered",
v.TotalShipped
"Total Shipped",
v.quantityremaining
"Quantity Left to Ship",
CASE
WHEN v.TotalShipped
= 0
THEN 'Not Shipped'
WHEN v.QuantityOrdered
= v.TotalShipped
THEN 'Completed'
WHEN v.TotalShipped
> v.QuantityOrdered
THEN 'Over Shipped'
ELSE 'Partially Shipped'
END "Ship Status"
FROM VShippedQty v
LEFT JOIN tblItem item
ON v.itemID = item.itemID
#1. Output
|
dbo.SQL4 |
||||||||
|
Order ID |
Order Date |
Customer Name |
Item ID |
Item Name |
Quantity Ordered |
Total Shipped |
Quantity Left to Ship |
Ship Status |
|
123000 |
|
|
A23441 |
New York City Monopoly Game Collector’s Edition |
8 |
8 |
0 |
Completed |
|
123000 |
|
|
A34665 |
Boggle Deluxe 5x5 |
30 |
32 |
-2 |
Over Shipped |
|
123000 |
|
|
B67123 |
Cranium WOW Edition |
5 |
5 |
0 |
Completed |
|
200335 |
|
Phillips, K. |
A23441 |
New York City Monopoly Game Collector’s Edition |
1 |
0 |
1 |
Not Shipped |
|
200335 |
|
Phillips, K. |
A34665 |
Boggle Deluxe 5x5 |
1 |
0 |
1 |
Not Shipped |
|
200335 |
|
Phillips, K. |
B67123 |
Cranium WOW Edition |
1 |
0 |
1 |
Not Shipped |
|
200335 |
|
Phillips, K. |
B67466 |
Diplomacy: Game of Negotiation, Cunning and Deceit. |
1 |
0 |
1 |
Not Shipped |
|
223344 |
|
Candriller, K. |
A23441 |
New York City Monopoly Game Collector’s Edition |
55 |
28 |
27 |
Partially Shipped |
|
223344 |
|
Candriller, K. |
A23771 |
Catch Phrase Game Music Edition |
15 |
15 |
0 |
Completed |
|
223344 |
|
Candriller, K. |
A34665 |
Boggle Deluxe 5x5 |
100 |
110 |
-10 |
Over Shipped |
|
223344 |
|
Candriller, K. |
A34882 |
Perudo |
35 |
35 |
0 |
Completed |
|
223344 |
|
Candriller, K. |
B67123 |
Cranium WOW Edition |
25 |
13 |
12 |
Partially Shipped |
|
223344 |
|
Candriller, K. |
B67466 |
Diplomacy: Game of Negotiation, Cunning and Deceit. |
15 |
0 |
15 |
Not Shipped |
|
300221 |
|
|
A23771 |
Catch Phrase Game Music Edition |
1 |
1 |
0 |
Completed |
|
300221 |
|
|
A34665 |
Boggle Deluxe 5x5 |
1 |
1 |
0 |
Completed |
|
300221 |
|
|
A34882 |
Perudo |
1 |
1 |
0 |
Completed |
|
300221 |
|
|
B67123 |
Cranium WOW Edition |
1 |
1 |
0 |
Completed |
|
300221 |
|
|
B78244 |
Chicks Battle the Dudes Board Game |
1 |
1 |
0 |
Completed |
|
400001 |
|
Jones, M. |
C26133 |
Knowledge Management: Create a Learning Organization |
1 |
0 |
1 |
Not Shipped |
|
445511 |
|
Jones, M. |
C34122 |
A Game of Strategy, Negotiation and Excitement for Office Retreats |
3 |
3 |
0 |
Completed |
|
450137 |
|
Rodriguez, K. |
A34665 |
Boggle Deluxe 5x5 |
10 |
10 |
0 |
Completed |
|
450137 |
|
Rodriguez, K. |
A34882 |
Perudo |
50 |
15 |
35 |
Partially Shipped |
|
450137 |
|
Rodriguez, K. |
B67123 |
Cranium WOW Edition |
21 |
29 |
-8 |
Over Shipped |
|
450137 |
|
Rodriguez, K. |
C26133 |
Knowledge Management: Create a Learning Organization |
1 |
0 |
1 |
Not Shipped |
|
450137 |
|
Rodriguez, K. |
C34122 |
A Game of Strategy, Negotiation and Excitement for Office Retreats |
6 |
0 |
6 |
Not Shipped |
|
567123 |
|
Rodriguez, K. |
C26133 |
Knowledge Management: Create a Learning Organization |
1 |
0 |
1 |
Not Shipped |
|
651222 |
|
|
A34665 |
Boggle Deluxe 5x5 |
5 |
5 |
0 |
Completed |
|
651222 |
|
|
A34882 |
Perudo |
16 |
16 |
0 |
Completed |
|
651222 |
|
|
B78244 |
Chicks Battle the Dudes Board Game |
21 |
21 |
0 |
Completed |
|
671100 |
|
Jones, M. |
C29179 |
Managing Change: The Game for an Executive Retreat |
1 |
1 |
0 |
Completed |
|
781206 |
|
Argiento, B. |
B67466 |
Diplomacy: Game of Negotiation, Cunning and Deceit. |
1 |
0 |
1 |
Not Shipped |
|
892211 |
|
Dao, P. |
C26133 |
Knowledge Management: Create a Learning Organization |
15 |
0 |
15 |
Not Shipped |
|
892211 |
|
Dao, P. |
C29179 |
Managing Change: The Game for an Executive Retreat |
10 |
6 |
4 |
Partially Shipped |
|
892211 |
|
Dao, P. |
C34122 |
A Game of Strategy, Negotiation and Excitement for Office Retreats |
8 |
5 |
3 |
Partially Shipped |
|
980001 |
|
Guili, M. |
C29179 |
Managing Change: The Game for an Executive Retreat |
3 |
3 |
0 |
Completed |
|
980001 |
|
Guili, M. |
C34122 |
A Game of Strategy, Negotiation and Excitement for Office Retreats |
2 |
2 |
0 |
Completed |
#2. Have any orders been over shipped? List the name of the customer, orderid, itemid, description of
the item and the quantity overshipped for any orders
where more items have been shipped for that order than were actually ordered.
#2. Code
CREATE VIEW
SELECT ol.orderID "OrderID",
ord.OrderDate,
cust.LastName + ',
'+ SUBSTRING(cust.firstname,1,1)+'.' "CustomerName",
ol.itemID "ItemID",
ol.quantity "QuantityOrdered",
ISNULL(SUM(sl.QtyShipped), 0) "TotalShipped",
ol.quantity - ISNULL(SUM(sl.QtyShipped), 0)
"quantityremaining"
FROM TblOrderLine ol
LEFT JOIN TblOrder ord
ON ord.orderID = ol.OrderID
LEFT JOIN TblShipLine sl
ON sl.ItemID = ol.ItemID AND
sl.OrderID = ol.OrderID
LEFT JOIN TblCustomer cust
ON ord.customerID = cust.customerID
GROUP BY ol.orderID,
cust.LastName,
cust.FirstName,
ol.ItemID,
ord.OrderDate,
ol.quantity
SELECT v.orderid "Order ID",
v.CustomerName
"Customer Name",
v.ItemID "Item
ID",
item.description
"Item Name",
v.QuantityOrdered
"Quantity Ordered",
v.TotalShipped
"Total Shipped",
v.TotalShipped-v.QuantityOrdered
"Quantity Overshipped"
FROM VShippedQty v
LEFT JOIN tblItem item
ON v.itemID = item.itemID
WHERE v.TotalShipped > v.QuantityOrdered
#2. Output
|
dbo.SQL4 |
||||||
|
Customer Name |
Order ID |
Item ID |
Item Name |
Quantity Ordered |
Total Shipped |
Quantity Overshipped |
|
|
123000 |
A34665 |
Boggle Deluxe 5x5 |
30 |
32 |
2 |
|
Candriller, K. |
223344 |
A34665 |
Boggle Deluxe 5x5 |
100 |
110 |
10 |
|
Rodriguez, K. |
450137 |
B67123 |
Cranium WOW Edition |
21 |
29 |
8 |