@@ -0,0 +1,4 @@ | |||
Select * | |||
FROM vendors | |||
INNER JOIN invoices | |||
ON vendors.vendors_id = invoice.vendors_id; |
@@ -0,0 +1,3 @@ | |||
Select * | |||
FROM vendors | |||
INNER JOIN invoices; |
@@ -0,0 +1,9 @@ | |||
SELECT | |||
vendor_name, | |||
invoice_number, | |||
invoice_date, | |||
invoice_total - payment_total - credit_total AS balance_due | |||
From vendors JOIN invoices | |||
on vendors.vendors_id = invoice.vendors_id | |||
WHERE invoice_total - payment_total - credit_total <> | |||
ORDER BY vendor_name ASC; |
@@ -0,0 +1,9 @@ | |||
SELECT | |||
vendor_name, | |||
invoice_number, | |||
invoice_date, | |||
invoice_total - payment_total - credit_total AS balance_due | |||
From vendors v, | |||
invoices i | |||
WHERE invoice_total IS NULL | |||
ORDER BY vendor_name ASC; |
@@ -0,0 +1,6 @@ | |||
SELECT vendor_name, | |||
default_account_number AS default_account, | |||
account_description AS description | |||
FROM vendors v JOIN general_ledger_accounts g | |||
ON vendors.default_account_number = general_ledger_accounts.default_account_number | |||
ORDER BY default_account, vendor_name; |
@@ -0,0 +1,6 @@ | |||
SELECT vendor_name, | |||
default_account_number AS default_account, | |||
account_description AS description | |||
FROM vendors v, | |||
general_ledger_accounts g | |||
ORDER BY default_account, vendor_name; |
@@ -0,0 +1,13 @@ | |||
SELECT vendor_name, | |||
invoice_date, | |||
invoice_number, | |||
invoice_sequence AS li_sequence, | |||
line_item_amount AS li_amount | |||
FROM vendors v JOIN invoices i, | |||
ON v.vendors_id = i.verdors_id | |||
JOIN Invoice_Line_Items li | |||
ON i.invoice_id = li.invoice.id | |||
ORDER BY vendor_name, | |||
invoice_date, | |||
invoice_number, | |||
invoice_sequence; |
@@ -0,0 +1,12 @@ | |||
SELECT vendor_name, | |||
invoice_date, | |||
invoice_number, | |||
invoice_sequence AS li_sequence, | |||
line_item_amount AS li_amount | |||
FROM vendors v, | |||
invoices i, | |||
Invoice_Line_Items l | |||
ORDER BY vendor_name, | |||
invoice_date, | |||
invoice_number, | |||
invoice_sequence; |
@@ -0,0 +1,6 @@ | |||
SELECT vendor_id, | |||
concat(vendor_contact_first_name, vendor_contact_last_name) AS contact_name | |||
FROM vendor_contacts v JOIN vendor_contacts v1 | |||
ON v.vendor_id <> v1.vendors_id v1 | |||
AND v.vendor_contact_last_name = v1.vendor_contact_last_name | |||
ORDER BY v.vendor_contact_last_name ASC; |
@@ -0,0 +1,6 @@ | |||
SELECT vendor_id, | |||
vendor_name, | |||
concat(vendor_contact_first_name, vendor_contact_last_name) AS contact_name | |||
FROM vendor v | |||
WHERE vendor_id = vendor_contact_last_name | |||
ORDER BY vendor_contact_last_name; |
@@ -0,0 +1,7 @@ | |||
SELECT account_number, | |||
account_description, | |||
invoice_id | |||
FROM invoice_line_items i LEFT OUTER JOIN general_ledger_accounts g | |||
ON i.account_number = g.account_number | |||
WHERE invoice_id = NULL | |||
ORDER BY g.account_number ASC; |
@@ -0,0 +1,10 @@ | |||
SELECT vendor_name, vendor_state | |||
FROM vendors v1 JOIN vendors v2 | |||
ON v1.vendor_name = v2.vendor_name | |||
WHERE v1.vendor_state = "CA" otherwise v1.vendor_state = "OUTSIDE CA" | |||
UNION | |||
SELECT vendor_name, vendor_state | |||
FROM vendors v1 JOIN vendors v2 | |||
ON v1.vendor_name = v2.vendor_name | |||
WHERE v1.vendor_state = "CA" otherwise v1.vendor_state = "OUTSIDE CA" | |||
ORDER BY vendor_name ASC; |
@@ -0,0 +1,4 @@ | |||
INSERT INTO terms | |||
(terms_id,terms_description,terms_due_days) | |||
VALUES | |||
(6,'Net due 120 days',120); |
@@ -0,0 +1,2 @@ | |||
UPDATE terms | |||
SET terms_description = 'Net due 125 days'; |
@@ -0,0 +1,3 @@ | |||
use ap; | |||
DELETE FROM terms | |||
WHERE terms_id = 6; |
@@ -0,0 +1,2 @@ | |||
INSERT INTO invoices VALUES | |||
(DEFAULT, 32, 'AX-014-027', '2018-01-08', 454.58, 0, 0, 2,'2018-08-31', NULL) |
@@ -0,0 +1,3 @@ | |||
INSERT INTO invoices_line_items VALUES | |||
(123,1, 160, 180.23, 'HARD DRIVE'), | |||
(123,2, 527, 254.35, 'Exchange Server update'); |
@@ -0,0 +1,6 @@ | |||
use ap; | |||
select * from invoices | |||
UPDATE invoices | |||
SET credit_total = invoice_total * 0.1 | |||
payement_total = invoice_total - credit_total | |||
WHERE invoice_id = ''; |
@@ -0,0 +1,4 @@ | |||
use ap; | |||
UPDATE vendors | |||
SET account_number = 403 | |||
WHERE vendor_id = 44; |
@@ -0,0 +1 @@ | |||
select * from invoices |
@@ -0,0 +1,8 @@ | |||
use ap; | |||
SELECT vendor_id, vendor_name, vendor_state | |||
FROM vendors | |||
WHERE NOT EXISTS | |||
(SELECT * | |||
FROM invoices | |||
WHERE vendor_id = vendors.vendor_id); |
@@ -0,0 +1,7 @@ | |||
SELECT vendor_name, invoice_number, invoice_total | |||
FROM vendors JOIN invoices | |||
ON vendors.vendor_id = invoices.vendor_id | |||
WHERE invoice_total < ANY | |||
(SELECT invoice_total | |||
FROM invoices | |||
WHERE vendor_id = 115); |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT vendor_name, invoice_number, invoice_total | |||
FROM invoices i JOIN vendors v ON i.vendor_id = v.vendor_id | |||
WHERE invoice_total > ALL | |||
(SELECT invoice_total | |||
FROM invoices | |||
WHERE vendor_id = 34) | |||
ORDER BY vendor_name; |
@@ -0,0 +1,6 @@ | |||
USE AP; | |||
SELECT v.vendor_id, vendor_name, vendor_state | |||
FROM vendors v LEFT JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
WHERE i.vendor_id IS NULL | |||
ORDER BY v.vendor_id; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT vendor_id, vendor_name, vendor_state | |||
FROM vendors | |||
WHERE vendor_id NOT IN | |||
(SELECT DISTINCT vendor_id | |||
FROM invoices) | |||
ORDER BY vendor_id; |
@@ -0,0 +1,9 @@ | |||
USE AP; | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE vendor_id IN | |||
(SELECT vendor_id | |||
FROM vendors | |||
WHERE vendor_state = 'CA') | |||
ORDER BY invoice_date; |
@@ -0,0 +1,7 @@ | |||
USE AP; | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices JOIN vendors | |||
ON invoices.vendor_id = vendors.vendor_id | |||
WHERE vendor_state = 'CA' | |||
ORDER BY invoice_date; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_total > | |||
(SELECT AVG(invoice_total) | |||
FROM invoices) | |||
ORDER BY invoice_total; |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
DELETE FROM general_ledger_accounts | |||
WHERE account_number = 306 | |||
@@ -0,0 +1,9 @@ | |||
USE AP; | |||
SELECT invoice_number, vendor_name, invoice_due_date, | |||
invoice_total - payment_total - credit_total | |||
AS balance_due | |||
FROM vendors v JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
WHERE invoice_total - payment_total - credit_total > 0 | |||
ORDER BY invoice_due_date DESC |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT invoice_number, line_item_amount, | |||
line_item_description | |||
FROM invoices JOIN invoice_line_items line_items | |||
ON invoices.invoice_id = line_items.invoice_id | |||
WHERE account_number = 540 | |||
ORDER BY invoice_date |
@@ -0,0 +1,3 @@ | |||
SELECT vendor_name, customer_last_name, customer_first_name, vendor_state AS state, vendor_city AS city FROM vendors v | |||
JOIN om.customers c | |||
ON v.vendor_zip_code = c.customer_zip ORDER BY state, city |
@@ -0,0 +1,4 @@ | |||
SELECT customer_first_name, customer_last_name | |||
FROM customers c JOIN employees e | |||
ON c.customer_first_name = e.first_name | |||
AND c.customer_last_name = e.last_name |
@@ -0,0 +1,9 @@ | |||
USE ap; | |||
SELECT DISTINCT v1.vendor_name, v1.vendor_city, | |||
v1.vendor_state | |||
FROM vendors v1 JOIN vendors v2 | |||
ON v1.vendor_city = v2.vendor_city AND | |||
v1.vendor_state = v2.vendor_state AND | |||
v1.vendor_name <> v2.vendor_name | |||
ORDER BY v1.vendor_state, v1.vendor_city; |
@@ -0,0 +1,9 @@ | |||
use ap; | |||
SELECT vendor_name, invoice_number, invoice_date, line_item_amount, account_description | |||
FROM vendors v | |||
JOIN invoices i | |||
ON v.vendor_id = i.vendor_id JOIN invoice_line_items li | |||
ON i.invoice_id = li.invoice_id JOIN general_ledger_accounts gl | |||
ON li.account_number = gl.account_number WHERE invoice_total - payment_total - credit_total > 0 | |||
ORDER BY vendor_name, line_item_amount DESC |
@@ -0,0 +1,6 @@ | |||
USE ap; | |||
SELECT invoice_number, vendor_name | |||
FROM vendors v, invoices i | |||
WHERE v.vendor_id = i.vendor_id | |||
ORDER BY invoice_number |
@@ -0,0 +1,11 @@ | |||
USE ap; | |||
SELECT vendor_name, invoice_number, invoice_date, | |||
line_item_amount, account_description | |||
FROM vendors v, invoices i, invoice_line_items li, | |||
general_ledger_accounts gl | |||
WHERE v.vendor_id = i.vendor_id | |||
AND i.invoice_id = li.invoice_id | |||
AND li.account_number = gl.account_number | |||
AND invoice_total - payment_total - credit_total > 0 | |||
ORDER BY vendor_name, line_item_amount DESC |
@@ -0,0 +1,6 @@ | |||
use ap; | |||
SELECT vendor_name, invoice_number, invoice_total | |||
FROM vendors LEFT JOIN invoices | |||
ON vendors.vendor_id = invoices.vendor_id | |||
ORDER BY vendor_name |
@@ -0,0 +1,8 @@ | |||
use ex; | |||
SELECT department_name, d.department_number, last_name | |||
FROM departments d LEFT JOIN employees e | |||
ON d.department_number = e.department_number | |||
ORDER BY department_name; |
@@ -0,0 +1,6 @@ | |||
use ex; | |||
SELECT department_name, e.department_number, last_name | |||
FROM departments d RIGHT JOIN employees e | |||
ON d.department_number = e.department_number | |||
ORDER BY department_name |
@@ -0,0 +1,9 @@ | |||
use ex; | |||
SELECT department_name, last_name, project_number | |||
FROM departments d LEFT JOIN employees e | |||
ON d.department_number = e.department_number LEFT JOIN projects p | |||
ON e.employee_id = p.employee_id | |||
ORDER BY department_name, last_name |
@@ -0,0 +1,8 @@ | |||
use ex; | |||
SELECT department_name, last_name, project_number | |||
FROM departments d JOIN employees e | |||
ON d.department_number = e.department_number | |||
LEFT JOIN projects p | |||
ON e.employee_id = p.employee_id | |||
ORDER BY department_name, last_name |
@@ -0,0 +1,8 @@ | |||
USE EX; | |||
SELECT department_name, last_name, project_number | |||
FROM departments | |||
JOIN employees USING (department_number) | |||
LEFT JOIN projects USING (employee_id) | |||
ORDER BY department_nameuse ex; | |||
@@ -0,0 +1,8 @@ | |||
USE EX; | |||
SELECT department_name, last_name, project_number | |||
FROM departments | |||
JOIN employees USING (department_number) | |||
LEFT JOIN projects USING (employee_id) | |||
ORDER BY department_nameuse ex; | |||
@@ -0,0 +1,4 @@ | |||
SELECT concat(vendor_contact_last_name, ' ',vendor_contact_first_name) AS full_name | |||
FROM vendors | |||
WHERE vendor_contact_last_name REGEXP 'A' OR 'B' OR 'C' OR 'E' | |||
ORDER BY vendor_contact_last_name ASC; |
@@ -0,0 +1,7 @@ | |||
USE AP; | |||
SELECT invoice_number, vendor_name, invoice_due_date, | |||
invoice_total - payment_total - credit_total AS balance_due | |||
FROM vendors v JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
WHERE invoice_total - payment_total - credit_total > 0 | |||
ORDER BY invoice_due_date DESC; |
@@ -0,0 +1,6 @@ | |||
USE AP; | |||
SELECT vendor_name, customer_last_name, customer_first_name, | |||
vendor_state AS state, vendor_city AS city | |||
FROM vendors v JOIN om.customers c | |||
ON v.vendor_zip_code = c.customer_zip | |||
ORDER BY state, city ASC; |
@@ -0,0 +1,10 @@ | |||
USE AP; | |||
SELECT vendor_name, invoice_number, invoice_date, account_description, line_item_amount | |||
FROM vendors v JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
JOIN invoice_line_items li | |||
ON i.invoice_id = li.invoice_id | |||
JOIN general_ledger_accounts ge | |||
on li.account_number = ge.account_number | |||
WHERE invoice_total - payment_total - credit_total > 0 | |||
ORDER BY vendor_name, line_item_amount DESC; |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
SELECT vendor_name, invoice_number, invoice_total | |||
FROM vendors v LEFT JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
ORDER BY vendor_name ASC |
@@ -0,0 +1,6 @@ | |||
USE ex; | |||
SELECT department_name, d.department_name, last_name | |||
FROM departments d LEFT JOIN employees e | |||
ON d.department_number = e.department_number | |||
ORDER BY department_name ASC; | |||
@@ -0,0 +1,7 @@ | |||
USE ex; | |||
SELECT department_name, last_name, project_number | |||
FROM departments d JOIN employees e | |||
ON d.department_number = e.department_number | |||
LEFT JOIN projects p | |||
ON e.employee_id = p.employee_id | |||
ORDER BY department_name, last_name ASC; |
@@ -0,0 +1,6 @@ | |||
USE ex; | |||
SELECT department_name, last_name, project_number | |||
FROM departments | |||
NATURAL JOIN employees | |||
LEFT JOIN projects USING (employee_id) | |||
ORDER BY department_name ASC; |
@@ -0,0 +1,6 @@ | |||
USE ex; | |||
SELECT departments.department_number, department_name, employee_id, last_name | |||
FROM departments,employees | |||
ORDER BY departments.department_number ASC; | |||
/* a cross join */ |
@@ -0,0 +1,11 @@ | |||
USE ex; | |||
SELECT 'Active' AS source, invoice_number, invoice_date, invoice_total | |||
FROM active_invoices | |||
WHERE invoice_date >= '2018_06_01' | |||
UNION | |||
SELECT 'Paid' AS source, invoice_number, invoice_date, invoice_total | |||
FROM paid_invoices | |||
WHERE invoice_date >= '2018_06_01' | |||
ORDER BY invoice_total DESC; | |||
/* union |
@@ -0,0 +1,10 @@ | |||
USE AP; | |||
SELECT 'Active' AS source, invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_date >= '2018_06_01' | |||
UNION | |||
SELECT 'Paid' AS source, invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_date >= '2018_06_01' | |||
ORDER BY invoice_total DESC; |
@@ -0,0 +1,3 @@ | |||
SELECT vendor_name, vendor_contact_last_name, vendor_contact_first_name | |||
FROM vendors | |||
ORDER BY vendor_contact_last_name, vendor_contact_first_name ASC; |
@@ -0,0 +1,21 @@ | |||
USE AP; | |||
SELECT invoice_number, vendor_name, '33% PAYMENT' AS payment_type, | |||
invoice_total AS total, invoice_total * 0.333 AS payment | |||
FROM invoices JOIN vendors | |||
ON invoices.vendor_id = vendors.vendor_id | |||
WHERE invoice_total > 10000 | |||
UNION | |||
SELECT invoice_number, vendor_name, '50% PAYMENT' AS payment_type, | |||
invoice_total AS total, invoice_total * 0.500 AS payment | |||
FROM invoices JOIN vendors | |||
ON invoices.vendor_id = vendors.vendor_id | |||
WHERE invoice_total BETWEEN 500 AND 10000 | |||
UNION | |||
SELECT invoice_number, vendor_name, 'FULL PAYMENT' AS payment_type, | |||
invoice_total AS total, invoice_total AS payment | |||
FROM invoices JOIN vendors | |||
ON invoices.vendor_id = vendors.vendor_id | |||
WHERE invoice_total < 500 | |||
ORDER BY payment_type, vendor_name, invoice_number; | |||
/* 3 unions van 4 tabellen |
@@ -0,0 +1,9 @@ | |||
11 | |||
32020.42 | |||
USE AP; | |||
SELECT COUNT(*) AS number_of_invoices, | |||
SUM(invoice_total - payment_total - credit_total) AS total_due | |||
FROM invoices | |||
WHERE invoice_total - payment_total - credit_total > 0; |
@@ -0,0 +1,12 @@ | |||
After 1/1/2018 | |||
114 | |||
37966.19 | |||
6.00 | |||
use ap; | |||
SELECT 'After 1/1/2018' AS selection_date, | |||
COUNT(*) AS number_of_invoices, | |||
MAX(invoice_total) AS highest_invoice_total, | |||
MIN(invoice_total) AS lowest_invoice_total | |||
FROM invoices | |||
WHERE invoice_date > '01/01/2018'; |
@@ -0,0 +1,7 @@ | |||
USE ap; | |||
SELECT COUNT(*) AS number_of_invoices, | |||
sum(invoice_total - payment_total - credit_total)AS total_due | |||
FROM invoices | |||
WHERE invoice_total - payment_total - credit_total > 0; | |||
@@ -0,0 +1,5 @@ | |||
USE ap; | |||
SELECT COUNT(*) AS number_of_invoices, | |||
sum(invoice_total - payment_total - credit_total)AS total_due | |||
FROM invoices | |||
WHERE invoice_total - payment_total - credit_total > 0; |
@@ -0,0 +1,5 @@ | |||
USE ap; | |||
SELECT COUNT(*) AS number_of_invoices, | |||
sum(invoice_total - payment_total - credit_total)AS total_due | |||
FROM invoices | |||
WHERE invoice_total - payment_total - credit_total > 0; |
@@ -0,0 +1,6 @@ | |||
USE ap; | |||
SELECT vendor_id,ROUND(AVG(invoice_total), 2) AS average_invoice_amount | |||
FROM invoices | |||
GROUP BY vendor_id | |||
HAVING AVG(invoice_total) > 2000 | |||
ORDER BY average_invoice_amount DESC; |
@@ -0,0 +1,30 @@ | |||
use ap; | |||
SELECT t1.vendor_state, vendor_name, t1.sum_of_invoices | |||
FROM | |||
(---- invoice totals by vendor ---- | |||
SELECT vendor_state, vendor_name, | |||
SUM(invoice_total) AS sum_of_invoices | |||
FROM vendors v JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
GROUP BY vendor_state, vendor_name | |||
) t1 | |||
JOIN | |||
( ---- top invoice totals by state ----- | |||
SELECT vendor_state, MAX(sum_of_invoices) | |||
AS sum_of_invoices | |||
FROM | |||
( ---- invoice totals by vendor ---- | |||
SELECT vendor_state, | |||
vendor_name, | |||
SUM(invoice_total) | |||
AS sum_of_invoices FROM vendors v JOIN invoices i | |||
ON v.vendor_id = | |||
i.vendor_id | |||
GROUP BY vendor_state, | |||
vendor_name ) t2 | |||
GROUP BY vendor_state ) t3 | |||
ON t1.vendor_state = t3.vendor_state AND t1.sum_of_invoices = | |||
t3.sum_of_invoices | |||
ORDER BY vendor_state |
@@ -0,0 +1,14 @@ | |||
use ap; | |||
SELECT vendor_state, | |||
MAX(sum_of_invoices) AS max_sum_of_invoices | |||
FROM | |||
( | |||
SELECT vendor_state, vendor_name, | |||
SUM(invoice_total) AS sum_of_invoices | |||
FROM vendors v JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
GROUP BY vendor_state, vendor_name | |||
) t | |||
GROUP BY vendor_state | |||
ORDER BY vendor_state |
@@ -0,0 +1,7 @@ | |||
use ap; | |||
SELECT vendor_name, MAX(invoice_date) AS latest_inv | |||
FROM vendors v | |||
LEFT JOIN invoices i ON v.vendor_id = i.vendor_id | |||
GROUP BY vendor_name | |||
ORDER BY latest_inv DESC; |
@@ -0,0 +1,4 @@ | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_date BETWEEN '2018-06-01' AND '2018-06-30' | |||
ORDER BY invoice_date DESC; |
@@ -0,0 +1,7 @@ | |||
use ap; | |||
SELECT vendor_name, | |||
(SELECT MAX(invoice_date) FROM invoices | |||
WHERE vendor_id = vendors.vendor_id) AS latest_inv | |||
FROM vendors | |||
ORDER BY latest_inv DESC; |
@@ -0,0 +1,3 @@ | |||
SELECT invoice_number,invoice_date,invoice_total | |||
FROM invoices | |||
ORDER BY invoice_total desc; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT 'After 1/1/2018' AS selection_date, | |||
COUNT(*) AS number_of_invoices, | |||
ROUND(AVG(invoice_total), 2) AS avg_invoice_amt, | |||
SUM(invoice_total) AS total_invoice_amt | |||
FROM invoices | |||
WHERE invoice_date > '2018-01-01'; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT 'After 1/1/2018' AS selection_date, | |||
COUNT(*) AS number_of_invoices, | |||
MAX(invoice_total) AS highest_invoice_total, | |||
MIN(invoice_total) AS lowest_invoice_total | |||
FROM invoices | |||
WHERE invoice_date > '2018-01-01'; |
@@ -0,0 +1,6 @@ | |||
USE AP; | |||
SELECT MIN(vendor_name) AS first_vendor, | |||
MAX(vendor_name) AS last_vendor, | |||
COUNT(vendor_name) AS number_of_vendors | |||
FROM vendors; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT COUNT(DISTINCT vendor_id) AS number_of_vendors, | |||
COUNT(vendor_id) AS number_of_invoices, | |||
ROUND(AVG(invoice_total), 2) AS avg_invoice_amt, | |||
SUM(invoice_total) AS total_invoice_amt | |||
FROM invoices | |||
WHERE invoice_date > '2018-01-01'; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT COUNT(DISTINCT vendor_id) AS number_of_vendors, | |||
COUNT(vendor_id) AS number_of_invoices, | |||
ROUND(AVG(invoice_total), 2) AS avg_invoice_amt, | |||
SUM(invoice_total) AS total_invoice_amt | |||
FROM invoices | |||
WHERE invoice_date > '2018-01-01'; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT vendor_id, ROUND(AVG(invoice_total), 2) | |||
AS average_invoice_amount | |||
FROM invoices | |||
GROUP BY vendor_id | |||
HAVING AVG(invoice_total) > 2000 | |||
ORDER BY average_invoice_amount DESC; |
@@ -0,0 +1,9 @@ | |||
USE AP; | |||
SELECT vendor_name, vendor_state, | |||
ROUND(AVG(invoice_total), 2) AS average_invoice_amount | |||
FROM vendors JOIN invoices | |||
ON vendors.vendor_id = invoices.vendor_id | |||
GROUP BY vendor_name | |||
HAVING AVG(invoice_total) > 2000 | |||
ORDER BY average_invoice_amount DESC; |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
SELECT vendor_id, COUNT(*) AS invoice_qty | |||
FROM invoices | |||
GROUP BY vendor_id; |
@@ -0,0 +1,8 @@ | |||
use ap; | |||
SELECT vendor_id, invoice_number, invoice_total | |||
FROM invoices i | |||
WHERE invoice_total > (SELECT AVG(invoice_total) | |||
FROM invoices | |||
WHERE vendor_id = i.vendor_id) | |||
ORDER BY vendor_id, invoice_total; |
@@ -0,0 +1,3 @@ | |||
SELECT invoice_id,invoice_total, credit_total + payment_total AS total | |||
FROM invoices | |||
WHERE invoice_id = 18; |
@@ -0,0 +1,7 @@ | |||
SELECT vendor_state, vendor_city, COUNT(*) AS invoice_qty, | |||
ROUND(AVG(invoice_total), 2) AS invoice_avg | |||
FROM invoices JOIN vendors | |||
ON invoices.vendor_id = vendors.vendor_id | |||
GROUP BY vendor_state, vendor_city | |||
HAVING COUNT(*) >= 2 | |||
ORDER BY vendor_state, vendor_city |
@@ -0,0 +1,9 @@ | |||
USE AP; | |||
SELECT vendor_name, | |||
COUNT(*) AS invoice_qty, | |||
ROUND(AVG(invoice_total), 2) AS invoice_avg | |||
FROM vendors JOIN invoices | |||
ON vendors.vendor_id = invoices.vendor_id | |||
GROUP BY vendor_name HAVING AVG(invoice_total) > 500 | |||
ORDER BY invoice_qty DESC; |
@@ -0,0 +1,8 @@ | |||
USE AP; | |||
SELECT vendor_name, COUNT(*) AS invoice_qty, | |||
ROUND(AVG(invoice_total), 2) AS invoice_avg | |||
FROM vendors JOIN invoices | |||
ON vendors.vendor_id = invoices.vendor_id WHERE invoice_total > 500 | |||
GROUP BY vendor_name | |||
ORDER BY invoice_qty DESC; |
@@ -0,0 +1,12 @@ | |||
USE AP; | |||
SELECT | |||
invoice_date, | |||
COUNT(*) AS invoice_qty, | |||
SUM(invoice_total) AS invoice_sum | |||
FROM invoices | |||
GROUP BY invoice_date | |||
HAVING invoice_date BETWEEN '2018-05-01' AND '2018-05-31' | |||
AND COUNT(*) > 1 | |||
AND SUM(invoice_total) > 100 | |||
ORDER BY invoice_date DESC; |
@@ -0,0 +1,10 @@ | |||
SELECT | |||
invoice_date, | |||
COUNT(*) AS invoice_qty, | |||
SUM(invoice_total) AS invoice_sum | |||
FROM invoices | |||
WHERE invoice_date BETWEEN '2018-05-01' AND '2018-05-31' | |||
GROUP BY invoice_date | |||
HAVING COUNT(*) > 1 | |||
AND SUM(invoice_total) > 100 | |||
ORDER BY invoice_date DESC |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
INSERT INTO invoices VALUES | |||
(115, 97, '456789', '2018-08-01', | |||
8344.50, 0, 0, 1, '2018-08-31', NULL) |
@@ -0,0 +1,6 @@ | |||
USE AP; | |||
UPDATE invoices | |||
SET payment_date = '2018-09-21', | |||
payment_total = 19351.18 | |||
WHERE invoice_number = '97/522' |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
UPDATE invoices | |||
SET terms_id = 1 | |||
WHERE vendor_id = 95 |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
UPDATE invoices | |||
SET credit_total = credit_total + 100 | |||
WHERE invoice_number = '97/522' |
@@ -0,0 +1,7 @@ | |||
USE AP; | |||
DELETE FROM invoice_line_items | |||
WHERE invoice_id IN | |||
(SELECT invoice_id | |||
FROM invoices | |||
WHERE vendor_id = 115) |
@@ -0,0 +1,3 @@ | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_total > 50000; |
@@ -0,0 +1,2 @@ | |||
DELETE FROM invoice_line_items | |||
WHERE invoice_id = 12 |
@@ -0,0 +1,3 @@ | |||
SELECT DISTINCT vendor_state, vendor_city | |||
FROM vendors | |||
ORDER BY vendor_state DESC; |
@@ -0,0 +1,5 @@ | |||
use ap; | |||
SELECT invoice_number, vendor_name | |||
FROM vendors INNER JOIN invoices | |||
ON vendors.vendor_id = invoices.vendor_id | |||
ORDER BY invoice_number; |
@@ -0,0 +1,7 @@ | |||
USE AP; | |||
SELECT invoice_number, vendor_name, invoice_due_date, | |||
invoice_total - payment_total - credit_total AS balance_due | |||
FROM vendors v JOIN invoices i | |||
ON v.vendor_id = i.vendor_id | |||
WHERE invoice_total - payment_total - credit_total > 0 | |||
ORDER BY invoice_due_date DESC; |