Add 2024-02-07 stuff
This commit is contained in:
parent
69bfe2b052
commit
348828018c
1 changed files with 49 additions and 1 deletions
|
@ -59,7 +59,7 @@ USE AP
|
|||
-- it doesn't matter which column, so * is fine
|
||||
SELECT COUNT(*)
|
||||
FROM Invoices
|
||||
WHERE VendorId = 123
|
||||
WHERE VendorID = 123
|
||||
|
||||
-- How many invoices in 2019
|
||||
SELECT COUNT(*) TotalInvoices
|
||||
|
@ -76,3 +76,51 @@ WHERE YEAR(InvoiceDueDate) = 2020
|
|||
-- sum, average, min, and max invoice totals
|
||||
SELECT SUM(InvoiceTotal) SumOfInvoices, AVG(InvoiceTotal) AvgInvoice, MIN(InvoiceTotal) SmallestInvoice, MAX(InvoiceTotal) LargestInvoice
|
||||
FROM Invoices
|
||||
|
||||
----------------
|
||||
-- 2024-02-07 --
|
||||
----------------
|
||||
|
||||
-- Get vendors with 3 or more invoices
|
||||
SELECT i.VendorID, v.VendorName, COUNT(*)
|
||||
FROM Invoices i
|
||||
JOIN Vendors v ON i.VendorID = v.VendorID
|
||||
GROUP BY i.VendorID, v.VendorName
|
||||
HAVING COUNT(*) >= 3
|
||||
ORDER BY i.VendorID
|
||||
|
||||
-- CHAPTER 6 --
|
||||
|
||||
-- Get vendors with no invoices
|
||||
SELECT VendorID
|
||||
FROM Vendors
|
||||
WHERE VendorID NOT IN (SELECT VendorID
|
||||
FROM Invoices)
|
||||
ORDER BY VendorID
|
||||
|
||||
-- Sub-queries aren't ideal, they perform worse than JOINs. With a JOIN:
|
||||
SELECT v.VendorID
|
||||
FROM Vendors v
|
||||
LEFT JOIN Invoices i ON v.VendorID = i.VendorID
|
||||
WHERE InvoiceID IS NULL
|
||||
|
||||
|
||||
-- Get all invoices > average invoice total
|
||||
-- Usually can't get rid of the sub-query for aggregate stuff like this
|
||||
SELECT InvoiceID
|
||||
FROM Invoices
|
||||
WHERE InvoiceTotal > (SELECT AVG(InvoiceTotal)
|
||||
FROM Invoices)
|
||||
ORDER BY InvoiceTotal
|
||||
|
||||
-- Get all invoices > average invoice total by vendor
|
||||
-- It's got a sub-query, but it only runs once so the performance isn't *that* bad
|
||||
SELECT TOP 10 InvoiceID, InvoiceTotal, i.VendorID, AvgTotal
|
||||
FROM Invoices i
|
||||
JOIN (SELECT VendorID, AVG(InvoiceTotal) AvgTotal
|
||||
FROM Invoices
|
||||
GROUP BY VendorID) AvgTotal ON i.VendorID = AvgTotal.VendorID
|
||||
WHERE InvoiceTotal >
|
||||
(SELECT AVG(InvoiceTotal)
|
||||
FROM Invoices)
|
||||
ORDER BY i.VendorID, InvoiceTotal
|
Loading…
Reference in a new issue