0

PSQL noob here. I'm trying to create a table similar to the following:

name foo    bar    baz     foo_per_bar      foo_bar_per_baz
joe  28     11     18      = foo/bar      = foo_per_bar/baz

and thus far my schema looks like:

CREATE TABLE foobars (
id: serial primary key,
foo: Integer,
bar: Integer,
baz: Integer,
foo_per_bar: ??,
foo_bars_per_baz: ??
);

Is such a thing supported? If not, how could I go about doing it? Create a separate table that performs the calculations and reference it in foobars?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Vidro3
  • 192
  • 2
  • 11
  • 2
    You can use a view for that –  Dec 06 '16 at 21:53
  • do you mind expounding on that a bit more? I'm really new to postgres – Vidro3 Dec 06 '16 at 22:42
  • 1
    Take a look at [this](http://stackoverflow.com/questions/8250389/computed-calculated-columns-in-postgresql) post. It's the same idea. – Chapon Dec 06 '16 at 23:41
  • This is against the [3NF](https://en.wikipedia.org/wiki/Third_normal_form#.22Nothing_but_the_key.22) rule: "[Every] non-key [attribute] must provide a fact about the key, the whole key, and nothing but the key." – Adam Dec 06 '16 at 23:44

0 Answers0