Re: if-clause to an exiting statement

Lists: pgsql-general
From: Kobi Biton <kobi(at)comns(dot)co(dot)il>
To: pgsql-general(at)postgresql(dot)org
Subject: if-clause to an exiting statement
Date: 2010-12-07 09:19:53
Message-ID: 1291713593.2075.64.camel@kbiton-lap-ub
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

hi i am a newbie to sql statments , I am running postgres 8.1 with
application called opennms version 1.8.5 due to an application bug
queries that I execute aginst the DB which returns raw-count=0 are being
ignored and will not process a certain trigger I need to process.

My question is : Can I use an if-clause into my statement (see below)
which will check if the returned raw-count =0 then will return
raw-count=1 ?
----------------------------------------------------------------------------------
SELECT a.eventuei AS _eventuei,
a.nodeid AS _nodeid,
a.ipaddr AS _ipaddr,
now() AS _ts
FROM events a
WHERE
a.eventuei='uei.opennms.org/comns/backup-success-trap' AND
(eventcreatetime &gt; now() - interval '10 minutes')
----------------------------------------------------------------------------------

Thanks!


From: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
To: Kobi Biton <kobi(at)comns(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: if-clause to an exiting statement
Date: 2010-12-07 10:13:41
Message-ID: AANLkTikKaXWX7V=oo4SZEs0kgz01o2-7K6v8-fP-zkQ3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

lookup CASE WHEN END in docs.

--
GJ


From: "kobi(dot)biton" <kobi(at)comns(dot)co(dot)il>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: if-clause to an exiting statement
Date: 2010-12-07 11:58:46
Message-ID: 1291723126437-3295641.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


hi thanks for the reply I did look at the CASE statement however cannot seem
to alter the returned row-count ...

CASE WHEN (@@ROW-COUNT = 0) THEN

[what do I write here?] @@ROW-COUNT = 1?

END
--
View this message in context: http://postgresql.1045698.n5.nabble.com/if-clause-to-an-exiting-statement-tp3295519p3295641.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: "kobi(dot)biton" <kobi(at)comns(dot)co(dot)il>
Subject: Re: if-clause to an exiting statement
Date: 2010-12-07 15:36:49
Message-ID: 201012070736.49868.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Tuesday 07 December 2010 3:58:46 am kobi.biton wrote:
> hi thanks for the reply I did look at the CASE statement however cannot
> seem to alter the returned row-count ...
>
> CASE WHEN (@@ROW-COUNT = 0) THEN
>
> [what do I write here?] @@ROW-COUNT = 1?
>
> END
> --
> View this message in context:
> http://postgresql.1045698.n5.nabble.com/if-clause-to-an-exiting-statement-t
>p3295519p3295641.html Sent from the PostgreSQL - general mailing list
> archive at Nabble.com.

See below:

test=> SELECT count(*) from bool_test ;
count
-------
33
(1 row)

test=> SELECT count(*) ,
case count(*) when 0
then 1
else
count(*)
end
from
bool_test ;

count | count
-------+-------
33 | 33
(1 row)

test=> SELECT count(*) ,
case count(*) when 0
then 1
else
count(*)
end
from
bool_test
where
ifd=0;

count | count
-------+-------
0 | 1
(1 row)

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com


From: Kobi Biton <kobi(at)comns(dot)co(dot)il>
To: adrian(dot)klaver(at)gmail(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: if-clause to an exiting statement
Date: 2010-12-07 16:04:39
Message-ID: 1291737879.17275.6.camel@kbiton-lap-ub
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Adrian hi,

Thanks for the reply can you please show me how to incorporate the below
into my below statement ?

SELECT a.eventuei AS _eventuei,
a.nodeid AS _nodeid,
a.ipaddr AS _ipaddr,
now() AS _ts
FROM events a
WHERE
eventuei='uei.opennms.org/comns/backup-success-trap' AND
(eventcreatetime &gt; now() - interval '10 minutes')

Thanks!
Kobi

On Tue, 2010-12-07 at 07:36 -0800, Adrian Klaver wrote:
> On Tuesday 07 December 2010 3:58:46 am kobi.biton wrote:
> > hi thanks for the reply I did look at the CASE statement however cannot
> > seem to alter the returned row-count ...
> >
> > CASE WHEN (@@ROW-COUNT = 0) THEN
> >
> > [what do I write here?] @@ROW-COUNT = 1?
> >

> > END
> > --
> > View this message in context:
> > http://postgresql.1045698.n5.nabble.com/if-clause-to-an-exiting-statement-t
> >p3295519p3295641.html Sent from the PostgreSQL - general mailing list
> > archive at Nabble.com.
>
> See below:
>
> test=> SELECT count(*) from bool_test ;
> count
> -------
> 33
> (1 row)
>
> test=> SELECT count(*) ,
> case count(*) when 0
> then 1
> else
> count(*)
> end
> from
> bool_test ;
>
> count | count
> -------+-------
> 33 | 33
> (1 row)
>
> test=> SELECT count(*) ,
> case count(*) when 0
> then 1
> else
> count(*)
> end
> from
> bool_test
> where
> ifd=0;
>
> count | count
> -------+-------
> 0 | 1
> (1 row)
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)gmail(dot)com
>

--
Kobi Biton
Com N S Ltd.

Mobile: +972 (54) 8017668


From: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
To: kobi(dot)biton <kobi(at)comns(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: *****SPAM***** Re: if-clause to an exiting statement
Date: 2010-12-07 16:51:09
Message-ID: 174FCAE5-82B6-44A0-8826-939CA0747BD6@elevated-dev.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Dec 7, 2010, at 4:58 AM, kobi.biton wrote:
>
> hi thanks for the reply I did look at the CASE statement however cannot seem
> to alter the returned row-count ...

Well, yeah. The row count is the count of rows returned. If there are no rows matched by the query, then what exactly do you expect to happen? Set the row count to 1, so that the application then tries to access the 1st row of 0???

If you need some dummy row returned even in the case where there's no match, then you'll have to construct your query that way...

--
Scott Ribe
scott_ribe(at)elevated-dev(dot)com
http://www.elevated-dev.com/
(303) 722-0567 voice


From: Kobi Biton <kobi(at)comns(dot)co(dot)il>
To: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: *****SPAM***** Re: if-clause to an exiting statement
Date: 2010-12-07 16:58:46
Message-ID: 1291741126.17275.12.camel@kbiton-lap-ub
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Scott hi,

I know it does not sound logic however I do need to set the row count
to 1 in case row count returns 0 , can you show how to add that case
clause and dummy line in my below code ?

SELECT a.eventuei AS _eventuei,
a.nodeid AS _nodeid,
a.ipaddr AS _ipaddr,
now() AS _ts
FROM events a
WHERE
a.eventuei='uei.opennms.org/comns/backup-success-trap' AND
(eventcreatetime &gt; now() - interval '10 minutes')

Thanks !!!!
Kobi

On Tue, 2010-12-07 at 09:51 -0700, Scott Ribe wrote:
> On Dec 7, 2010, at 4:58 AM, kobi.biton wrote:
> >
> > hi thanks for the reply I did look at the CASE statement however cannot seem
> > to alter the returned row-count ...

>
> Well, yeah. The row count is the count of rows returned. If there are no rows matched by the query, then what exactly do you expect to happen? Set the row count to 1, so that the application then tries to access the 1st row of 0???
>
> If you need some dummy row returned even in the case where there's no match, then you'll have to construct your query that way...
>
> --
> Scott Ribe
> scott_ribe(at)elevated-dev(dot)com
> http://www.elevated-dev.com/
> (303) 722-0567 voice
>
>
>
>
>

--
Kobi Biton
Com N S Ltd.

Mobile: +972 (54) 8017668


From: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
To: Kobi Biton <kobi(at)comns(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: *****SPAM***** Re: if-clause to an exiting statement
Date: 2010-12-07 17:44:07
Message-ID: 28AACBE0-0644-43B4-9D85-2DA118707ED8@elevated-dev.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Dec 7, 2010, at 9:58 AM, Kobi Biton wrote:
>
> I know it does not sound logic however I do need to set the row count
> to 1 in case row count returns 0

Perhaps I didn't make myself clear: you can't do that. The only thing you can do is make sure your query returns a row, and in the case where it currently doesn't return a row I have absolutely no idea what it would be that you would need to return.

If it would be acceptable to always return some hard-wired dummy row in addition to the 0 or more rows that match the current query, then you could use a UNION to add the dummy row to the selection. Otherwise, perhaps the real problem is that you do not have a matching event in the database and the real solution is to add such an event.

In your original post you referred to an application bug where a trigger does not run if the row count is 0. It's hard for me to imagine how it's a bug to not take action when there is no event that needs processing...

--
Scott Ribe
scott_ribe(at)elevated-dev(dot)com
http://www.elevated-dev.com/
(303) 722-0567 voice


From: Kobi Biton <kobi(at)comns(dot)co(dot)il>
To: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: *****SPAM***** Re: if-clause to an exiting statement
Date: 2010-12-07 18:18:56
Message-ID: 1291745936.17275.15.camel@kbiton-lap-ub
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I know it does not make sens "application bug" however consider the
following scenarion , looking at the Statement I sent I would like to
check if over the last 10 minutes a certain type of event was logged and
if NOT (row-count=0) then I would like to trigger and action.

hope it makes more sense.

Kobi.
On Tue, 2010-12-07 at 10:44 -0700, Scott Ribe wrote:
> On Dec 7, 2010, at 9:58 AM, Kobi Biton wrote:
> >
> > I know it does not sound logic however I do need to set the row count
> > to 1 in case row count returns 0
>
> Perhaps I didn't make myself clear: you can't do that. The only thing you can do is make sure your query returns a row, and in the case where it currently doesn't return a row I have absolutely no idea what it would be that you would need to return.
>
> If it would be acceptable to always return some hard-wired dummy row in addition to the 0 or more rows that match the current query, then you could use a UNION to add the dummy row to the selection. Otherwise, perhaps the real problem is that you do not have a matching event in the database and the real solution is to add such an event.
>
> In your original post you referred to an application bug where a trigger does not run if the row count is 0. It's hard for me to imagine how it's a bug to not take action when there is no event that needs processing...
>
> --
> Scott Ribe
> scott_ribe(at)elevated-dev(dot)com
> http://www.elevated-dev.com/
> (303) 722-0567 voice
>
>
>
>
>

--
Kobi Biton
Com N S Ltd.

Mobile: +972 (54) 8017668


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Kobi Biton <kobi(at)comns(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: if-clause to an exiting statement
Date: 2010-12-07 18:40:25
Message-ID: 4CFE7F99.5020004@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 12/07/2010 08:04 AM, Kobi Biton wrote:
> Adrian hi,
>
> Thanks for the reply can you please show me how to incorporate the below
> into my below statement ?
>
> SELECT a.eventuei AS _eventuei,
> a.nodeid AS _nodeid,
> a.ipaddr AS _ipaddr,
> now() AS _ts
> FROM events a
> WHERE
> eventuei='uei.opennms.org/comns/backup-success-trap' AND
> (eventcreatetime&gt; now() - interval '10 minutes')
>
> Thanks!
> Kobi
>

>>
>

Not sure this is what you want but here, reminder count(*) can have
performance issues for large values of count():

SELECT a.eventuei AS _eventuei,
case count(*) when 0
then 1
else
count(*)
end
AS _ct,
a.nodeid AS _nodeid,
a.ipaddr AS _ipaddr,
now() AS _ts
FROM events a
WHERE
eventuei='uei.opennms.org/comns/backup-success-trap' AND
(eventcreatetime&gt; now() - interval '10 minutes')

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com


From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: if-clause to an exiting statement
Date: 2010-12-17 04:27:09
Message-ID: ieeoqt$j8e$2@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 2010-12-07, Kobi Biton <kobi(at)comns(dot)co(dot)il> wrote:
> hi i am a newbie to sql statments , I am running postgres 8.1 with
> application called opennms version 1.8.5 due to an application bug
> queries that I execute aginst the DB which returns raw-count=0 are being
> ignored and will not process a certain trigger I need to process.

I think you want this:

ORIGINAL QUERY
union
select DUMMY ROW DATA
where
not exists ( ORIGINAL QUERY )

you need to return something to get a rowcount of 1 this is what the
dummy row data provides. the "where not exists" part blocks the dummy
row data when the main query returns something.

--
⚂⚃ 100% natural


From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: if-clause to an exiting statement
Date: 2010-12-17 13:03:58
Message-ID: 4D0B5FBE.6010708@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Jasen Betts wrote:
> On 2010-12-07, Kobi Biton <kobi(at)comns(dot)co(dot)il> wrote:
>
>> hi i am a newbie to sql statments , I am running postgres 8.1 with
>> application called opennms version 1.8.5 due to an application bug
>> queries that I execute aginst the DB which returns raw-count=0 are being
>> ignored and will not process a certain trigger I need to process.
>>
>
> I think you want this:
>
> ORIGINAL QUERY
> union
> select DUMMY ROW DATA
> where
> not exists ( ORIGINAL QUERY )
>
>
> you need to return something to get a rowcount of 1 this is what the
> dummy row data provides. the "where not exists" part blocks the dummy
> row data when the main query returns something.
>
>

Simple enough, but I suspect it runs the same query twice, so I hope
it's not to expensive. I wonder what the app is/was doing with the
vacuous single row or will do with the dummy data row? Seems the
app/trigger simply needs to know the execution of the query was
successful irrespective of the actual row count - or is that the bug
with "1.8.5"?