Intermediate values and unprivileged users

From: Mark Morgan Lloyd <markMLl(dot)pgsql-general(at)telemetry(dot)co(dot)uk>
To: pgsql-general(at)PostgreSQL(dot)org
Subject: Intermediate values and unprivileged users
Date: 2009-09-24 14:04:09
Message-ID: h9fu8p$7ng$1@pye-srv-01.telemetry.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

My apologies if this is an FAQ or considered too general.

I have a query like this which returns a single result:

SELECT (
(SELECT avg(rel_pressure) as avg4
FROM weather
WHERE now() - datetime <= '4 hours'
) -
(SELECT avg(rel_pressure) as avg24
FROM weather
WHERE now() - datetime <= '24 hours'
)
) AS diff;

What I want to be able to do is have a slightly more complex query like
this:

SELECT (
(SELECT avg(rel_pressure) AS avg4
FROM weather
WHERE now() - datetime <= '4 hours'
) -
(SELECT avg(rel_pressure) AS avg24
FROM weather
WHERE now() - datetime <= '24 hours'
)
) AS diff,
CASE
WHEN diff < -0.1 THEN 'Falling'
WHEN diff > 0.1 THEN 'Rising'
ELSE 'Stable'
END AS tendency;

i.e. the result should be a single row with two columns. Unfortunately
all my attempts so far tell me that column "diff" does not exist.

Now in most cases I could wing it using a view or temporary table, but
in the current one users of the database will not have creation rights:
they have to get their queries right or (eventually) use a high-level
language.

Is there a "good" way to get round this?

Server is 8.2 on Linux x86, queries from psql.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

Responses

Browse pgsql-general by date

  From Date Subject
Next Message 纪晓曦 2009-09-24 14:18:27 What is the difference of foreign key?
Previous Message Grzegorz Jaśkiewicz 2009-09-24 13:58:52 Re: Understanding 'could not read block'