pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedselect multiple immediate values, but in multiple rows
3+ messages / 3 participants
[nested] [flat]
* select multiple immediate values, but in multiple rows
@ 2005-03-15 06:18 Luca Pireddu <lucap@shaw.ca>
0 siblings, 2 replies; 3+ messages in thread
From: Luca Pireddu @ 2005-03-15 06:18 UTC (permalink / raw)
To: pgsql-sql
Hello all. I'd like to write a query does a set subtraction A - B, but A is
is a set of constants that I need to provide in the query as immediate
values. I thought of something like
select a from (1,2,3.4)
except
select col_name from table;
but I don't know the syntax to specify my set of constants. I thought of
doing
select 1 union select 2 union select 3 union ... except ...
but I figure there must be a better way. The size of the set would be
anywhere between 1 and about 5 or 6 elements. Is there a better way to do
this?
Thanks.
Luca
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: select multiple immediate values, but in multiple rows
@ 2005-03-15 08:18 Richard Huxton <dev@archonet.com>
parent: Luca Pireddu <lucap@shaw.ca>
1 sibling, 0 replies; 3+ messages in thread
From: Richard Huxton @ 2005-03-15 08:18 UTC (permalink / raw)
To: lucap@personainternet.com; +Cc: pgsql-sql
Luca Pireddu wrote:
> Hello all. I'd like to write a query does a set subtraction A - B, but A is
> is a set of constants that I need to provide in the query as immediate
> values. I thought of something like
>
> select a from (1,2,3.4)
> except
> select col_name from table;
richardh=> SELECT * FROM foo;
a
---
1
2
3
(3 rows)
richardh=> SELECT * FROM foo WHERE a NOT IN (1,2);
a
---
3
(1 row)
Run some tests with nulls in the column and the constant list too so you
understand what happens in those cases.
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: select multiple immediate values, but in multiple rows
@ 2005-03-15 14:20 PFC <lists@boutiquenumerique.com>
parent: Luca Pireddu <lucap@shaw.ca>
1 sibling, 0 replies; 3+ messages in thread
From: PFC @ 2005-03-15 14:20 UTC (permalink / raw)
To: lucap@personainternet.com; pgsql-sql
You need a function like this :
CREATE OR REPLACE FUNCTION array_srf( integer[] ) RETURNS SETOF integer
LANGUAGE PLPGSQL etc... AS
$$
DECLARE
_data ALIAS FOR $1;
_i INTEGER;
BEGIN
FOR _i IN 1..icount(_data) LOOP
RETURN NEXT _data[_i];
END LOOP;
RETURN;
END;
$$
select * from array_srf('{1,2,3,4}');
array_srf
-----------
1
2
3
4
(4 lignes)
This will avoid you the UNION.
> Hello all. I'd like to write a query does a set subtraction A - B, but
> A is
> is a set of constants that I need to provide in the query as immediate
> values. I thought of something like
>
> select a from (1,2,3.4)
> except
> select col_name from table;
>
> but I don't know the syntax to specify my set of constants. I thought of
> doing
> select 1 union select 2 union select 3 union ... except ...
> but I figure there must be a better way. The size of the set would be
> anywhere between 1 and about 5 or 6 elements. Is there a better way to
> do
> this?
>
> Thanks.
>
> Luca
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2005-03-15 14:20 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-03-15 06:18 select multiple immediate values, but in multiple rows Luca Pireddu <lucap@shaw.ca>
2005-03-15 08:18 ` Richard Huxton <dev@archonet.com>
2005-03-15 14:20 ` PFC <lists@boutiquenumerique.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