Question

Write a SELECT statement that returns the sum of the largest unpaid invoices submitted by each...

Write a SELECT statement that returns the sum of the largest unpaid invoices submitted by each vendor. Use a derived table that returns MAX(InvoiceTotal) grouped by VendorID, filtering for invoices with a balance due.

TABLES:

Vendor

Columns: VendorID, VendorName, DefaultTermsID, DefaultAccountNo

Invoices

Columns: InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal, PaymentTotal, CreditTotal, TermsID

Homework Answers

Answer #1

Creating Vendor Table :

CREATE TABLE VENDOR (VENDORID INT PRIMARY KEY,VENDORNAME CHAR(20),DefaultTermsID char(20),DefaultAccountNo char(30));

Creating Invoices Table:

CREATE TABLE INVOICES (INVOICEID INT,VENDORID INT,INVOICENUMBER INT,INVOICEDATE DATE,INVOICETOTAL INT,PAYMENTTOTAL INT,CREDITTOTAL INT);

Adding Foreign Keys:

ALTER TABLE INVOICES ADD FOREIGN KEY (VENDORID) REFERENCES VENDOR(VENDORID);

Inserting values in Vendor Table:

INSERT INTO VENDOR VALUES (1,'Roman','SA1','786757890'),(2,'Adam','SA4','765431291'),(3,'Ding','SA5','976530189');

Inserting values in Invoices Table:

INSERT INTO INVOICES VALUES (100,1,1000,'2019-08-27',10000,8000,8000);
INSERT INTO INVOICES VALUES (100,1,1001,'2019-08-29',1000,8000,8000);
INSERT INTO INVOICES VALUES (100,1,1001,'2019-09-19',3000,8000,8000);
INSERT INTO INVOICES VALUES (101,2,1000,'2019-09-17',1000,100,2000);
INSERT INTO INVOICES VALUES (101,2,1001,'2019-09-18',2000,100,1000);
INSERT INTO INVOICES VALUES (102,3,1009,'2019-09-11',20000,1000,9000);
INSERT INTO INVOICES VALUES (102,3,1019,'2019-09-13',10000,2000,9000);

Displaying the Details of Vendor and Invoices Table:

SELECT * FROM VENDOR;

SELECT * FROM INVOICES;

Query Solution:

SELECT A.VENDORID,A.VENDORNAME,SUM(INVOICETOTAL)

FROM VENDOR A JOIN INVOICES B

ON A.VENDORID=B.VENDORID

GROUP BY VENDORID

HAVING SUM(INVOICETOTAL) = (SELECT MAX(UNPAID_DUE) FROM (SELECT SUM(INVOICETOTAL) AS UNPAID_DUE FROM INVOICES GROUP BY VENDORID) C );

Query Explanation:

Here First we are calculating sum(invoicetotal) group by each vendorid. And then we are finding the maximum of that after that we are joining the vendor and invoices based on vendorid and then by using having clause we are checking which vendorid's invoice total sum is equal to max(invoicetotal) that was calculated earlier.

Code and Output Screenshots:

Note : I have assumed that invoice total is unpaid balance so we are taking the maximum unpaid bill.

Note : If you have any queries please post a comment thanks a lot..always available to help you....

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Class-In Assignment 3: Chapters 4&5 How to retrieve data from two or more tables Exercises 1....
Class-In Assignment 3: Chapters 4&5 How to retrieve data from two or more tables Exercises 1. Write a SELECT statement that returns all columns from the Vendors table inner-joined with all columns from the Invoices table. 2. Write a SELECT statement that returns four columns: vendor_name vendor_name from the Vendors table invoice_number invoice_number from the Invoices table invoice_date invoice_date from the Invoices table balance_due invoice_total minus payment_total minus credit_total from the Invoices table The result set should have one row...
in mysql 3. Write a SELECT statement that returns one row for each customer that has...
in mysql 3. Write a SELECT statement that returns one row for each customer that has orders with these columns:   The email_address column from the Customers table   The sum of the item price in the Order_Items table multiplied by the   quantity in the Order_Items table   The sum of the discount amount column in the Order_Items table   multiplied by the quantity in the Order_Items table   Sort the result set in descending sequence by the item price total for each customer.  
Write a SELECT statement that returns one row for each general ledger account number that contains...
Write a SELECT statement that returns one row for each general ledger account number that contains three columns: The account_description column from the General_Ledger_Accounts table The count of the items in the Invoice_Line_Items table that have the same account_number. The sum of the line_item_amount columns in the Invoice_Line_Items table that have the same account_number Return only those rows where the count of line items is greater than 1. This should return 10 rows. Group the result set by the account_description...
1. Write a SELECT statement to count SKUs for each department, i.e., a table of two...
1. Write a SELECT statement to count SKUs for each department, i.e., a table of two columns, departments and their SKU counts. use cape_codd; select department, count(*) from sku_data group by department ; 2. List buyers who is responsible for at least two SKUs, in three columns, department, buyer, sku count. Sort buyers by their department names in ascending order. select department, buyer, count(sku) from sku_data group by buyer ; 3. Write a SQL statement which returns all the orders....
Write an UPDATE statement that modifies the Invoices table. Change the terms_id column to 2 for...
Write an UPDATE statement that modifies the Invoices table. Change the terms_id column to 2 for each invoice that's for a vendor with a default_terms_id of 2.
Write a SELECT statement that uses aggregate window functions to calculate the order total for each...
Write a SELECT statement that uses aggregate window functions to calculate the order total for each musician and the order total for each musician by date. Return these columns: The musician_id column from the Orders table The order_date column from the Orders table The total amount for each order item in the Order_Items table The sum of the order totals for each musician The sum of the order totals for each musician by date (Hint: You can create a peer...
Write a SELECT statement that returns "Representative full name" from sales_rep table. Format "Representative full name"...
Write a SELECT statement that returns "Representative full name" from sales_rep table. Format "Representative full name" column with the rep_first_name column , a comma, a space and the rep_last name. Return only the sales representatives whose last name starts with "Mar" Write a SELECT statement that joins the sales_rep table to the sales_totals table using JOIN clause and returns these columns : "Representative Last name" : rep_last_name from sales_rep table " Year of sales " : sales_year column of sales_totals...
1.Write a SELECT statement that returns "Representative full name" from sales_rep table. Format "Representative full name"...
1.Write a SELECT statement that returns "Representative full name" from sales_rep table. Format "Representative full name" column with the rep_first_name column , a comma, a space and the rep_last name. Return only the sales representatives whose last name starts with "Mar" 2. Write a SELECT statement that  joins the sales_rep table to the sales_totals table using JOIN clause and returns these columns : "Representative Last name" : rep_last_name from sales_rep table " Year of sales " : sales_year column of sales_totals...
In SQL working with data types 1. Write a SELECT statement that returns these columns from...
In SQL working with data types 1. Write a SELECT statement that returns these columns from the Instructors table: a. The monthly salary (the AnnualSalary column divided by 12) b. A column that uses the CAST function to return the monthly salary with 1 digit to the right of the decimal point c. A column that uses the CONVERT function to return the monthly salary as an integer d. A column that uses the CAST function to return the monthly...
Write SELECT statement that returns the "Representative ID" and their highest sales total of that sales...
Write SELECT statement that returns the "Representative ID" and their highest sales total of that sales representative, grouped by the " Representative ID" . Representative ID    : rep_id column of rep_totals table "Highest sales of Representative " : maximum sales_total of that representative. Return only the rep_id whose highest sales_total is greater than 900000 Expected Output: Representative ID Highest sales of representative 1 1274856.38 2 978465.99 3 1132744.56