SELECT column WHERE (type = 'S' OR type = 'B') but perform different actions depending on whether type = 'S' or 'B'

From: litu16 <litumelendez(at)gmail(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: SELECT column WHERE (type = 'S' OR type = 'B') but perform different actions depending on whether type = 'S' or 'B'
Date: 2015-06-27 02:43:48
Message-ID: 1435373028918-5855422.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hi all, this is my table...

<http://postgresql.nabble.com/file/n5855422/screenshot.jpg>

I only want to select **one** "time" row, **either** WHERE "time_type" =
'Start' OR "time_type" = 'Break', **but only one**, the one that is at the
bottom row (descending) (ORDER BY "fn_serial" DESC LIMIT 1).

Im successfully doing it by using this Trigger Function...

* CREATE OR REPLACE FUNCTION timediff()
RETURNS trigger AS
$BODY$
DECLARE
prevtime character varying;

BEGIN
SELECT t.time FROM table_ebscb_spa_log04 t WHERE t.fn_name = NEW.fn_name
AND (t.time_type = 'Start' OR time_type = 'Break') ORDER BY t.fn_serial DESC
LIMIT 1 INTO prevtime;
IF NOT FOUND THEN
RAISE EXCEPTION USING MESSAGE = 'NOT FOUNDED';
ELSE
NEW.time_elapse := prevtime
END IF;
return NEW;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION timediff()
OWNER TO postgres;*

But in my script I would like to perform different actions depending on
whether "fn_type" = 'Start' OR "fn_type = 'Break', I mean where "prevtime"
variable came from, eg:

* IF "prevtime" came from "fn_type" = 'Start' THEN
RAISE EXCEPTION USING MESSAGE = 'PREVTIME CAME FROM START';
ELSIF "prevtime" came from "fn_type" = 'BREAK' THEN
RAISE EXCEPTION USING MESSAGE = 'PREVTIME CAME FROM BREAK';*

I can guess a way to do that, but its redundant, So I would like to ask
suggestions from good PostgreSQL fellow programmers.

Thanks Advanced.

--
View this message in context: http://postgresql.nabble.com/SELECT-column-WHERE-type-S-OR-type-B-but-perform-different-actions-depending-on-whether-type-S-or-B-tp5855422.html
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.

Browse pgsql-admin by date

  From Date Subject
Next Message litu16 2015-06-27 17:43:19 SELECT column WHERE time_type = 'Break' but only the rows that are after(below) time_type = 'Start'
Previous Message Kevin Grittner 2015-06-26 19:40:18 Re: Does VACUUM FULL need ANALYZE ?