Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

A schema and a couple of functions for tracking monetary loans in a PostgreSQL
database.

“I paid 42.3 units of money to John Doe.”

  select insert_exchange (
    'Yours Truly', 'John Doe', 'today', 42.3, 'Optional comment');

Aggregate what I’ve loaned/repaid to others and what they’ve loaned/repaid to
me (me being user id=5).

  select sum (e.value) as value, users.name as "from"
  from exchanges_with_user (5) as e
  join users on e.from_user_id = users.id
  group by users.name
  order by users.name;

 value |       from
-------+-------------------
   -10 | Jane Doe            She owes me 10 €.
     0 | John Doe            I don’t owe anything to him and vice versa.
   -99 | Steve Ballmer       He owes me 99 €.
    55 | Steve Jobs          I owe 55 € to him.

List exchanges in chronological order.

  select u_from.name as from, u_to.name as to,
         e.timestamp, e.value, e.comment
  from exchanges as e
  join users as u_from on e.from_user_id = u_from.id
  join users as u_to on e.to_user_id = u_to.id
  order by e.timestamp;

About

Track loans in a PostgreSQL database

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors