|
Preparing Invoices is Truly Fun
By Kalman Toth, M.Phil., M.Phil., MCDBA, MCITP
June 11, 2005
Businesses commonly accept purchase orders from each other. Upon
receipt of the PO the product(s) is shipped and the customer receives
an invoice to be paid. The SQL query below generates the invoicing
data which can be fed into a form reporting program for actual
invoice printing. The GROUP BY brings together all the details
for a particular PO. The WHERE clause narrows down the output.
USE AdventureWorks;
SELECT
InvoiceNo = convert(char(8),getdate(),112)+right('000'+convert(varchar,ROW_NUMBER() over (ORDER BY ph.PurchaseOrderID)),4),
Vendor=v.Name,
POID=ph.PurchaseOrderID,
Items=count(*),
SubTotal=convert(numeric(10,2),sum(subtotal)),
Tax=convert(numeric(10,2),sum(TaxAmt)),
Freight=convert(numeric(10,2),sum(Freight)),
Total='$'+convert(varchar,sum(TotalDue),1)
FROM
Purchasing.Vendor v
INNER JOIN
Purchasing.PurchaseOrderHeader ph
ON v.VendorID=ph.VendorID
INNER JOIN Purchasing.PurchaseOrderDetail pd
ON ph.PurchaseOrderID = pd.PurchaseOrderID
INNER JOIN Production.Product p
ON pd.ProductID = p.ProductID
WHERE
v.Name like ('P%') and datepart(month,ph.OrderDate)=5
GROUP BY
v.Name,ph.PurchaseOrderID
ORDER BY
v.Name,ph.PurchaseOrderID;
Here is the output:
| InvoiceNo |
Vendor |
POID |
Items |
SubTotal |
Tax |
Freight |
Total |
| 200910010002 |
Premier Sport, Inc. |
139 |
1 |
21558.08 |
1724.65 |
538.95 |
$23,821.67 |
| 200910010005 |
Premier Sport, Inc. |
297 |
1 |
21558.08 |
1724.65 |
538.95 |
$23,821.67 |
| 200910010009 |
Premier Sport, Inc. |
2489 |
1 |
21558.08 |
1724.65 |
538.95 |
$23,821.67 |
| 200910010013 |
Premier Sport, Inc. |
2568 |
1 |
21558.08 |
1724.65 |
538.95 |
$23,821.67 |
| 200910010017 |
Premier Sport, Inc. |
2647 |
1 |
21558.08 |
1724.65 |
538.95 |
$23,821.67 |
| 200910010021 |
Premier Sport, Inc. |
2726 |
1 |
21558.08 |
1724.65 |
538.95 |
$23,821.67 |
| 200910010003 |
Pro Sport Industries |
140 |
3 |
1055.57 |
84.45 |
26.39 |
$1,166.40 |
| 200910010006 |
Pro Sport Industries |
298 |
5 |
3444.37 |
275.55 |
86.11 |
$3,806.03 |
| 200910010010 |
Pro Sport Industries |
2490 |
3 |
1266.3 |
101.3 |
31.66 |
$1,399.26 |
| 200910010014 |
Pro Sport Industries |
2569 |
5 |
3318.37 |
265.47 |
82.96 |
$3,666.80 |
| 200910010018 |
Pro Sport Industries |
2648 |
5 |
3121.49 |
249.72 |
78.04 |
$3,449.25 |
| 200910010022 |
Pro Sport Industries |
2727 |
6 |
4261.95 |
340.96 |
106.55 |
$4,709.45 |
| 200910010004 |
Professional Athletic Consultants |
141 |
2 |
90344.1 |
7227.53 |
2258.6 |
$99,830.23 |
| 200910010007 |
Professional Athletic Consultants |
299 |
1 |
20980.58 |
1678.45 |
524.51 |
$23,183.54 |
| 200910010011 |
Professional Athletic Consultants |
2491 |
2 |
90344.1 |
7227.53 |
2258.6 |
$99,830.23 |
| 200910010015 |
Professional Athletic Consultants |
2570 |
3 |
195720.5 |
15657.64 |
4893.01 |
$216,271.18 |
| 200910010019 |
Professional Athletic Consultants |
2649 |
1 |
20980.58 |
1678.45 |
524.51 |
$23,183.54 |
| 200910010023 |
Professional Athletic Consultants |
2728 |
4 |
344883 |
27590.64 |
8622.08 |
$381,095.72 |
| 200910010001 |
Proseware, Inc. |
7 |
3 |
176056.7 |
14084.53 |
4401.42 |
$194,542.60 |
| 200910010008 |
Proseware, Inc. |
323 |
1 |
25334.93 |
2026.79 |
633.37 |
$27,995.09 |
| 200910010012 |
Proseware, Inc. |
2515 |
3 |
176056.7 |
14084.53 |
4401.42 |
$194,542.60 |
| 200910010016 |
Proseware, Inc. |
2594 |
2 |
66701.25 |
5336.1 |
1667.53 |
$73,704.88 |
| 200910010020 |
Proseware, Inc. |
2673 |
1 |
25334.93 |
2026.79 |
633.37 |
$27,995.09 |
| 200910010024 |
Proseware, Inc. |
2752 |
2 |
66701.25 |
5336.1 |
1667.53 |
$73,704.88 |
|
| The World Leader
in SQL Server Training |
 |
|