pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Calculated Fields in Postgres?
2+ messages / 2 participants
[nested] [flat]

* Calculated Fields in Postgres?
@ 2007-01-02 21:47 Jeanna Geier <jgeier@apt-cafm.com>
  2007-01-02 22:00 ` Re: Calculated Fields in Postgres? Andres Olarte <olarte.andres@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Jeanna Geier @ 2007-01-02 21:47 UTC (permalink / raw)
  To: pgsql-jdbc

Hello List!

Still trying to learn Postgres... =)  I have a question on how/where to
define a calculated field in a table...

Let's say I have a 'Replacement Cost' table with the following fields:

Original Cost
Original Date
Life Expectancy
Life Units

and I want to add a 'Mfg. Replace Date' field, which is a calculated field
with the following formula:

IF ('Life Units' = NAV)
 THEN (NAV)
ELSE
 ( IF ('Life Units' = 'Day(s)'')
     THEN (AddDate('Original Date,0,0,'Life Expectancy'))
ELSE
 ( IF 'Life Units'  = 'Month(s)'')
     THEN (AddDate('Original Date',0,'Life Expectancy',0))
ELSE
  ( IF 'Life Units' = 'Year(s)'')
     THEN (AddDate('Original Date','Life Expectancy',0.0))
ELSE
(today))))

--------------------------------------------------------

How/where do I define this calculated field?

Any help or guidance that anyone could provide will be greatly appreciated!

If any more information is necessary from my end, please let me know.

Thanks,
-Jeanna




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Calculated Fields in Postgres?
  2007-01-02 21:47 Calculated Fields in Postgres? Jeanna Geier <jgeier@apt-cafm.com>
@ 2007-01-02 22:00 ` Andres Olarte <olarte.andres@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Andres Olarte @ 2007-01-02 22:00 UTC (permalink / raw)
  To: Jeanna Geier <jgeier@apt-cafm.com>; +Cc: pgsql-jdbc

This is not a JDBC question, that's a basic SQL question, but any
way... You have some choices:

1- Use a view to show the data from the table plus the calculated fields.
2- Use a trigger to define the value of the calculated field on INSERT.

Which one you use depends on the usage scenario, basically how a
change of the mfg policy would affect you.  However, you seem to have
a problem with your table structure, so you should probably read a
little bit about normalization.  For example, it might make more sense
to convert everything to days, and forget about the 'Life Units'
field.

On 1/2/07, Jeanna Geier <jgeier@apt-cafm.com> wrote:
> Hello List!
>
> Still trying to learn Postgres... =)  I have a question on how/where to
> define a calculated field in a table...
>
> Let's say I have a 'Replacement Cost' table with the following fields:
>
> Original Cost
> Original Date
> Life Expectancy
> Life Units
>
> and I want to add a 'Mfg. Replace Date' field, which is a calculated field
> with the following formula:
>
> IF ('Life Units' = NAV)
>  THEN (NAV)
> ELSE
>  ( IF ('Life Units' = 'Day(s)'')
>      THEN (AddDate('Original Date,0,0,'Life Expectancy'))
> ELSE
>  ( IF 'Life Units'  = 'Month(s)'')
>      THEN (AddDate('Original Date',0,'Life Expectancy',0))
> ELSE
>   ( IF 'Life Units' = 'Year(s)'')
>      THEN (AddDate('Original Date','Life Expectancy',0.0))
> ELSE
> (today))))
>
> --------------------------------------------------------
>
> How/where do I define this calculated field?
>
> Any help or guidance that anyone could provide will be greatly appreciated!
>
> If any more information is necessary from my end, please let me know.
>
> Thanks,
> -Jeanna
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-01-02 22:00 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-01-02 21:47 Calculated Fields in Postgres? Jeanna Geier <jgeier@apt-cafm.com>
2007-01-02 22:00 ` Andres Olarte <olarte.andres@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox