0% found this document useful (0 votes)
18 views4 pages

Back Office at The Literary Society

Uploaded by

Khushi Chaudhry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Back Office at The Literary Society

Uploaded by

Khushi Chaudhry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C207 - Database Systems

Lesson 4 Problem Statement OFFICIA


L
(CLOSED
) \ NON-
SENSITI
Back Office @ The Literary Society VE

Rachel has requested you to operate the back office support functions although not directly
related to sales do enhance the customer satisfaction since all of the events experienced by
customers before and after a purchase are part of the customer experience. Your responsibilities
include IT support, inventory management, purchasing, billing of customers and tracking their
account settlements.

In discharging your responsibilities, you need to create and execute the following SQL queries on
the the Literary Society’s database.

Task 1

Before you can bill the schools, you need to generate a report like the sample shown below. The
report will help you determine the amount to invoice. You can only invoice if all items in an
order are delivered.

The list price is the selling price before discount. Sort your results by the second column in
ascending sequence. Use the column labels provided in the format of the sample report. Exclude
orders that have already been invoiced. (7 rows)

HINT: Use ‘IS NULL’ to test whether an order has been invoiced.

© School of Infocomm, Republic Polytechnic Page 1


C207 - Database Systems
Lesson 4 Problem Statement OFFICIA
L
(CLOSED
SELECT so.order_id AS Order_ID, ) \ NON-
si.invoice_id AS Invoice_ID, SENSITI
VE
s.school_name AS School_Name,
SUM(li.quantity * li.list_price) AS Total_Amount
FROM school_order so
INNER JOIN school s ON so.school_id = s.school_id
LEFT JOIN invoice si ON so.order_id = si.order_id
INNER JOIN line_item li ON so.order_id = li.order_id
WHERE si.invoice_id IS NULL
GROUP BY so.order_id, si.invoice_id, s.school_name
HAVING COUNT(li.quantity) = SUM(li.delivered)
ORDER BY 2 ASC;
Task 2
You can now invoice the schools based on the report you generated in Task 2. Minimise the
number of invoice per school. The new invoice number is an increment of 1 from the last invoice
number. Use today’s date as the invoice date for the invoices you are inserting.

Study the schema carefully and look for the Primary Key and Foreign Key contraints used to relate
the invoice and school_order tables. (HINT: For every invoice record you INSERT, you will need to
make the corresponding UPDATE in the school_order table).
DECLARE @last_invoice_id INT;
SELECT @last_invoice_id = MAX(invoice_id) FROM invoice;
INSERT INTO invoice (invoice_id, invoice_date, order_id)
SELECT (@last_invoice_id + ROW_NUMBER() OVER(ORDER BY so.order_id)) AS Invoice_ID,
GETDATE() AS Invoice_Date,
so.order_id
FROM school_order so
LEFT JOIN invoice si ON so.order_id = si.order_id
INNER JOIN line_item li ON so.order_id = li.order_id
WHERE si.invoice_id IS NULL
GROUP BY so.order_id, si.invoice_id
HAVING COUNT(li.quantity) = SUM(li.delivered);
Task 3
Check that the database reflects the invoices you generated in Task 2 by writing an SQL query.
Your query should display the results using the following format. Sort the results by the first two
columns. (5 rows)

SELECT invoice_id AS Invoice_ID,


invoice_date AS Invoice_Date,
order_id AS Order_ID
FROM invoice
ORDER BY 1, 2;
Instructions to Solving Questions in the Problem Statement

1. Use the syntax guide below to write your SQl.


2. Refer to the Back Office @ Literary Society schema diagram in your student resources to
understand the relationships amongst the tables.

© School of Infocomm, Republic Polytechnic Page 2


C207 - Database Systems
Lesson 4 Problem Statement OFFICIA
L
(CLOSED
3. Set up the schema based on the ) \ NON- schema script Back Office @ The Literary
Society. SENSITI
VE
4. Write your SQL statements and test them in Workbench.
5. Write your solution below the questions in this document.
6. Use the guide below to structure your SQL statements.

SYNTAX GUIDE

SYNTAX GUIDE
SELECT {DISTINCT} <column_name {AS} column_label, …>
FROM table_1
© INNER
School ofJOIN table_2
Infocomm, ON
Republic search condition ** number of inner join = n-1 tables
Polytechnic Page 3

WHERE <filter condition list)>


ORDER BY <column_1 {ASC/DESC}, column_2…>
C207 - Database Systems
Lesson 4 Problem Statement OFFICIA
L
(CLOSED
) \ NON-
SENSITI
VE

SYNTAX GUIDE
DELETE FROM table_name
WHERE condition;

SYNTAX GUIDE

DELETE table_A / table_A’s alias


FROM table_A
<INNER JOIN list>
WHERE <condition list>

SYNTAX GUIDE
UPDATE table_name
<INNER JOIN list>
SET column1 = value1, column2 = value2, ...
WHERE <condition list>

© School of Infocomm, Republic Polytechnic Page 4

You might also like