From a8a734e525e315e30d2e05fb45dd0865d5d17b97 Mon Sep 17 00:00:00 2001 From: Dora Su Date: Wed, 28 Nov 2018 23:23:30 -0500 Subject: [PATCH 1/3] Create Implementing Balance Sheets in PostgreSQL.md A gallery entry on making balance sheets using PostgreSQL. --- ...plementing Balance Sheets in PostgreSQL.md | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 content/feature/Implementing Balance Sheets in PostgreSQL.md diff --git a/content/feature/Implementing Balance Sheets in PostgreSQL.md b/content/feature/Implementing Balance Sheets in PostgreSQL.md new file mode 100644 index 0000000..a8d3bd6 --- /dev/null +++ b/content/feature/Implementing Balance Sheets in PostgreSQL.md @@ -0,0 +1,86 @@ +|Implementing Balance Sheets in PostgreSQL|November 28, 2018 | +|--|--| + +### Example +| Date | Title | Credit (+) | Debit (-) | Balance | +|--|--|--|--|--| +| 2018-10-31 | Halloween Candy | | 30 | -30 | +| 2018-11-30 | Salary and Tax | 4000 | 200 | 3770 | +| 2018-12-01 | Christmas Tree | | 200 | 3570 | + +### Description +Using balance sheets is a great way to keep track of assets and liabilities for both personal and business reasons. Spreadsheet software may be a good choice since it is simpler to access previous rows; however, this document entails the process of building a balance sheet in PostgreSQL in a simple manner. + +### Code +```sql +CREATE TABLE public."BalanceSheet" + ( + "Date" Date, + "Title" Text, + "Credit (+)" NUMERIC, + "Debit (-)" NUMERIC, + "Balance" NUMERIC + ); +INSERT INTO "BalanceSheet" + ( + "Date", + "Title", + "Credit (+)", + "Debit (-)", + "Balance" + ) + VALUES + ( + '2018-10-31', + 'Halloween Candy', + 0, + 30, + 0 + ) + , + ( + '2018-11-30', + 'Salary and Tax', + 4000, + 200, + 0 + ) + , + ( + '2018-12-02', + 'Christmas Tree', + 0, + 200, + 0 + ); + +WITH tt + AS (SELECT a."Date", + Sum(b."Credit (+)" - b."Debit (-)") Running_Total + FROM "BalanceSheet" a, + "BalanceSheet" b + WHERE ( b."Date" <= a."Date" ) + GROUP BY 1 + ORDER BY 1) +UPDATE "BalanceSheet" +SET "Balance" = tt.running_total +FROM tt +WHERE tt."Date" = "BalanceSheet"."Date"; + +SELECT * +FROM PUBLIC."BalanceSheet" +ORDER BY "Date" ASC; +``` +### Code Breakdown and Explanation +*BalanceSheet* contains columns for date, title, credit, debit, and balance. This makes it easy to view and understand the table's contents. + +*tt* is a temporary table used to calculate *running_total* of all of the previous entries' separate balances until the current entry. +>`Sum` is used to calculate this by subtracting the *Debits (-)* (money owed) from the *Credit (+)* (money gained) each month. + +>`GROUP BY` selects the data for aggregation (*running_total*). + +>`ORDER BY` sorts the data by *Date* so an accurate balance is calculated. + +>`WHERE` makes sure the *Balance* is only updated for the correct entry. + +Finally, the table is sorted by *Date* for easier interpretation using `ORDER BY`. From 3729cbd9cdefd96dc04b8850d1b965fa074c743f Mon Sep 17 00:00:00 2001 From: Dora Su Date: Wed, 28 Nov 2018 23:27:26 -0500 Subject: [PATCH 2/3] Update Implementing Balance Sheets in PostgreSQL.md --- content/feature/Implementing Balance Sheets in PostgreSQL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/feature/Implementing Balance Sheets in PostgreSQL.md b/content/feature/Implementing Balance Sheets in PostgreSQL.md index a8d3bd6..984a4ae 100644 --- a/content/feature/Implementing Balance Sheets in PostgreSQL.md +++ b/content/feature/Implementing Balance Sheets in PostgreSQL.md @@ -9,7 +9,7 @@ | 2018-12-01 | Christmas Tree | | 200 | 3570 | ### Description -Using balance sheets is a great way to keep track of assets and liabilities for both personal and business reasons. Spreadsheet software may be a good choice since it is simpler to access previous rows; however, this document entails the process of building a balance sheet in PostgreSQL in a simple manner. +Using balance sheets is a great way to keep track of assets and liabilities for both personal and business reasons. Spreadsheet software may be a good choice since it is simpler to access previous rows; however, this document entails the process of building a balance sheet in PostgreSQL in a simple manner. In this real-life example shown above, it is easy to interpret what the person has bought or where they have lost money and where they have gained money. ### Code ```sql From e8bbca501ee75d629967a0b1dd42db9eabe9007b Mon Sep 17 00:00:00 2001 From: Dora Su Date: Fri, 30 Nov 2018 00:23:01 -0500 Subject: [PATCH 3/3] Update Implementing Balance Sheets in PostgreSQL.md Title Updated. --- .../feature/Implementing Balance Sheets in PostgreSQL.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/content/feature/Implementing Balance Sheets in PostgreSQL.md b/content/feature/Implementing Balance Sheets in PostgreSQL.md index 984a4ae..a4eb49b 100644 --- a/content/feature/Implementing Balance Sheets in PostgreSQL.md +++ b/content/feature/Implementing Balance Sheets in PostgreSQL.md @@ -1,6 +1,7 @@ -|Implementing Balance Sheets in PostgreSQL|November 28, 2018 | -|--|--| - +--- +title: "Implementing Balance Sheets in PostgreSQL" +Date: "2018-11-30" +--- ### Example | Date | Title | Credit (+) | Debit (-) | Balance | |--|--|--|--|--|