| 1 | ===Advanced SQL Queries and Reports for Invoices |
| 2 | |
| 3 | **1. Report: Total Revenue Generated by Each Company in the Last Year** |
| 4 | |
| 5 | {{{ |
| 6 | SELECT |
| 7 | c.company_id, |
| 8 | c.name AS company_name, |
| 9 | SUM(i.total_amount) AS total_revenue |
| 10 | FROM |
| 11 | Company c |
| 12 | JOIN Invoice i ON c.company_id = i.company_id |
| 13 | WHERE |
| 14 | i.issue_date BETWEEN NOW() - INTERVAL '1 year' AND NOW() |
| 15 | GROUP BY |
| 16 | c.company_id, c.name |
| 17 | ORDER BY |
| 18 | total_revenue DESC; |
| 19 | }}} |
| 20 | |
| 21 | **2. Report: Invoices Issued to Each Client and Their Total Amount** |
| 22 | {{{ |
| 23 | SELECT |
| 24 | cl.client_id, |
| 25 | cl.name AS client_name, |
| 26 | COUNT(i.invoice_id) AS total_invoices, |
| 27 | SUM(i.total_amount) AS total_amount |
| 28 | FROM |
| 29 | Client cl |
| 30 | JOIN Invoice i ON cl.client_id = i.client_id |
| 31 | GROUP BY |
| 32 | cl.client_id, cl.name |
| 33 | ORDER BY |
| 34 | total_amount DESC; |
| 35 | }}} |
| 36 | |
| 37 | **3. Report: List of Overdue Invoices** |
| 38 | {{{ |
| 39 | SELECT |
| 40 | i.invoice_id, |
| 41 | i.issue_date, |
| 42 | i.due_date, |
| 43 | i.total_amount, |
| 44 | cl.name AS client_name, |
| 45 | c.name AS company_name |
| 46 | FROM |
| 47 | Invoice i |
| 48 | JOIN Client cl ON i.client_id = cl.client_id |
| 49 | JOIN Company c ON i.company_id = c.company_id |
| 50 | WHERE |
| 51 | i.due_date < NOW() AND i.status != 'Paid' |
| 52 | ORDER BY |
| 53 | i.due_date ASC; |
| 54 | }}} |
| 55 | |
| 56 | **4. Report: Clients with the Highest Invoice Payments** |
| 57 | {{{ |
| 58 | SELECT |
| 59 | cl.client_id, |
| 60 | cl.name AS client_name, |
| 61 | SUM(i.total_amount) AS total_paid |
| 62 | FROM |
| 63 | Client cl |
| 64 | JOIN Invoice i ON cl.client_id = i.client_id |
| 65 | WHERE |
| 66 | i.status = 'Paid' |
| 67 | GROUP BY |
| 68 | cl.client_id, cl.name |
| 69 | ORDER BY |
| 70 | total_paid DESC |
| 71 | LIMIT 10; |
| 72 | }}} |
| 73 | |
| 74 | **5. Report: Outstanding Invoices by Each Client** |
| 75 | {{{ |
| 76 | SELECT |
| 77 | cl.client_id, |
| 78 | cl.name AS client_name, |
| 79 | COUNT(i.invoice_id) AS outstanding_invoices, |
| 80 | SUM(i.total_amount) AS outstanding_amount |
| 81 | FROM |
| 82 | Client cl |
| 83 | JOIN Invoice i ON cl.client_id = i.client_id |
| 84 | WHERE |
| 85 | i.status = 'Unpaid' |
| 86 | GROUP BY |
| 87 | cl.client_id, cl.name |
| 88 | ORDER BY |
| 89 | outstanding_amount DESC; |
| 90 | }}} |
| 91 | |
| 92 | **6. Report: Monthly Invoice Revenue** |
| 93 | {{{ |
| 94 | SELECT |
| 95 | TO_CHAR(i.issue_date, 'YYYY-MM') AS month, |
| 96 | SUM(i.total_amount) AS total_revenue |
| 97 | FROM |
| 98 | Invoice i |
| 99 | GROUP BY |
| 100 | TO_CHAR(i.issue_date, 'YYYY-MM') |
| 101 | ORDER BY |
| 102 | month DESC; |
| 103 | }}} |
| 104 | |
| 105 | **7. Report: Top 5 Products/Services Sold by Revenue** |
| 106 | {{{ |
| 107 | SELECT |
| 108 | l.description AS product_name, |
| 109 | SUM(l.quantity * l.unit_price) AS total_revenue |
| 110 | FROM |
| 111 | LineItem l |
| 112 | GROUP BY |
| 113 | l.description |
| 114 | ORDER BY |
| 115 | total_revenue DESC |
| 116 | LIMIT 5; |
| 117 | }}} |
| 118 | |
| 119 | **8. Report: Invoices with the Most Line Items** |
| 120 | {{{ |
| 121 | SELECT |
| 122 | i.invoice_id, |
| 123 | i.issue_date, |
| 124 | i.total_amount, |
| 125 | COUNT(l.line_item_id) AS line_item_count |
| 126 | FROM |
| 127 | Invoice i |
| 128 | JOIN LineItem l ON i.invoice_id = l.invoice_id |
| 129 | GROUP BY |
| 130 | i.invoice_id, i.issue_date, i.total_amount |
| 131 | ORDER BY |
| 132 | line_item_count DESC |
| 133 | LIMIT 10; |
| 134 | }}} |
| 135 | |
| 136 | **9. Report: Average Time to Payment** |
| 137 | {{{ |
| 138 | SELECT |
| 139 | AVG(i.payment_date - i.issue_date) AS avg_days_to_payment |
| 140 | FROM |
| 141 | Invoice i |
| 142 | WHERE |
| 143 | i.status = 'Paid'; |
| 144 | }}} |
| 145 | |
| 146 | **10. Report: Invoices with Discounts Applied** |
| 147 | {{{ |
| 148 | SELECT |
| 149 | i.invoice_id, |
| 150 | i.issue_date, |
| 151 | i.total_amount, |
| 152 | i.discount_percentage, |
| 153 | (i.total_amount * i.discount_percentage / 100) AS discount_amount |
| 154 | FROM |
| 155 | Invoice i |
| 156 | WHERE |
| 157 | i.discount_percentage > 0 |
| 158 | ORDER BY |
| 159 | discount_amount DESC; |
| 160 | }}} |