title: Create a PostgreSQL function that becomes a formula field of a table retrieving related data from other table
body: <p>The example above can be done on a SQL Server. It is a function that performs the calculation on another table while getting the current table field Id to list data from other table, return a single value.</p>
  <p>Question: how to do the exact thing with PostgreSQL</p>
  <pre><code>
  SELECT TOP(5) * FROM Artists;
  
  +------------+------------------+--------------+-------------+
  | ArtistId   | ArtistName       | ActiveFrom   | CountryId   |
  |------------+------------------+--------------+-------------|
  | 1          | Iron Maiden      | 1975-12-25   | 3           |
  | 2          | AC/DC            | 1973-01-11   | 2           |
  | 3          | Allan Holdsworth | 1969-01-01   | 3           |
  | 4          | Buddy Rich       | 1919-01-01   | 6           |
  | 5          | Devin Townsend   | 1993-01-01   | 8           |
  +------------+------------------+--------------+-------------+
  
  
  SELECT TOP(5) * FROM Albums;
  
  +-----------+------------------------+---------------+------------+-----------+
  | AlbumId   | AlbumName              | ReleaseDate   | ArtistId   | GenreId   |
  |-----------+------------------------+---------------+------------+-----------|
  | 1         | Powerslave             | 1984-09-03    | 1          | 1         |
  | 2         | Powerage               | 1978-05-05    | 2          | 1         |
  | 3         | Singing Down the Lane  | 1956-01-01    | 6          | 3         |
  | 4         | Ziltoid the Omniscient | 2007-05-21    | 5          | 1         |
  | 5         | Casualties of Cool     | 2014-05-14    | 5          | 1         |
  +-----------+------------------------+---------------+------------+-----------+
  
  </code></pre>
  <p>The function</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE FUNCTION [dbo].[ufn_AlbumCount] (@ArtistId int)  
  RETURNS smallint
  AS  
  BEGIN  
      DECLARE @AlbumCount int;
      SELECT @AlbumCount = COUNT(AlbumId)
      FROM Albums
      WHERE ArtistId = @ArtistId; 
      RETURN @AlbumCount;
  END;
  GO
  </code></pre>
  <p>Now, (at SQL Server), after update the first table fields with <code>ALTER TABLE Artists ADD AlbumCount AS dbo.ufn_AlbumCount(ArtistId);</code> whe can list and get the following result.</p>
  <pre><code>+------------+------------------+--------------+-------------+--------------+
  | ArtistId   | ArtistName       | ActiveFrom   | CountryId   | AlbumCount   |
  |------------+------------------+--------------+-------------+--------------|
  | 1          | Iron Maiden      | 1975-12-25   | 3           | 5            |
  | 2          | AC/DC            | 1973-01-11   | 2           | 3            |
  | 3          | Allan Holdsworth | 1969-01-01   | 3           | 2            |
  | 4          | Buddy Rich       | 1919-01-01   | 6           | 1            |
  | 5          | Devin Townsend   | 1993-01-01   | 8           | 3            |
  | 6          | Jim Reeves       | 1948-01-01   | 6           | 1            |
  | 7          | Tom Jones        | 1963-01-01   | 4           | 3            |
  | 8          | Maroon 5         | 1994-01-01   | 6           | 0            |
  | 9          | The Script       | 2001-01-01   | 5           | 1            |
  | 10         | Lit              | 1988-06-26   | 6           | 0            |
  +------------+------------------+--------------+-------------+--------------+
  
  </code></pre>
  <p>but how to achieve this on postgresql?</p>