I'm converting some procedural code to SQL as an experiment. Here's the
c = a - b
if c < 0 then d = 'no'
else d = 'yes'
In SQL, I've got this:
select a, b, a - b as c,
case when a - b < 0 then 'no'
end as d
This is a trivial example, but you can see how I calculate a - b two
In reality, I have much nastier calculations and they happen more than
I'm looking for an elegant solution for this puzzle. I don't want to
repeat that a - b part over and over because I likely will need to
change how c gets defined and I don't want to have to change more than
one place in the code.
All I can come up with so far is to use a view and then another view on
top of that one:
create view v1 as
select a, b, a - b as c
create view v2 as
select a, b, c,
case when c < 0 then 'no'
end as d
This is better than the first solution because c is only defined in a
single place. Is this the best possible solution?
Thanks for the help.
|PgEast: 2011, CFP closes in three days||0|
|Upgrading using streaming replication||4|
|Finding last checkpoint time||12|
|Daisy chaining replication slaves ?||1|
|Re: shared data for different applications||1|
|[postgis-users] pg_dump -s should use add_geometrycolumn(...)||7|
|.psql_history file is messed up and control-r does not work||2|
|In one of negative test row-level trigger results into loop||2|
|Reduce the time to know trigger_file's existence||0|