pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSELECT UNION into a text[]
3+ messages / 3 participants
[nested] [flat]
* SELECT UNION into a text[]
@ 2018-10-09 16:32 Alexander Farber <alexander.farber@gmail.com>
2018-10-09 16:39 ` Re: SELECT UNION into a text[] David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Alexander Farber @ 2018-10-09 16:32 UTC (permalink / raw)
To: pgsql-general
Good evening,
I am trying to SELECT ARRAY_AGG into an array from 2 tables.
But unfortunately here is what I get in PostgreSQL 10.5:
SELECT ARRAY_AGG(hashed)
FROM words_nouns
WHERE added > TO_TIMESTAMP(1539100913)
UNION
SELECT ARRAY_AGG(hashed)
FROM words_verbs
WHERE added > TO_TIMESTAMP(1539100913);
array_agg
-----------------------------------------------------
{noun1,noun2,noun3}
{verb1,verb2}
And thus I can not assign it to the _added variable in my custom function:
CREATE OR REPLACE FUNCTION words_get_added(
in_visited integer,
OUT out_json jsonb
) RETURNS jsonb AS
$func$
DECLARE
_added text[];
BEGIN
-- create array with words added to dictionary since in_visited
timestamp
IF in_visited > 0 THEN
_added := (
SELECT ARRAY_AGG(hashed)
FROM words_nouns
WHERE added > TO_TIMESTAMP(in_visited)
UNION
SELECT ARRAY_AGG(hashed)
FROM words_verbs
WHERE added > TO_TIMESTAMP(in_visited)
);
IF CARDINALITY(_added) > 0 THEN
out_json := jsonb_build_object('added', _added);
END IF;
END IF;
END
And the assignment results in the error message:
www java[1082]: org.postgresql.util.PSQLException: ERROR: more than one
row returned by a subquery used as an expression| Where: SQL statement
"SELECT (| SELECT ARRAY_AGG(hashed) |
FROM words_nouns | WHERE added >
TO_TIMESTAMP(in_visited)| UNION|
SELECT ARRAY_AGG(hashed) | FROM words_verbs |
WHERE added > TO_TIMESTAMP(in_visited)|
)"|PL/pgSQL function words_get_added(integer) line 7 at assignment
Please help me to modify my SELECT UNION so that I get just 1 row as result:
{noun1,noun2,noun3,verb1,verb2}
Regards
Alex
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: SELECT UNION into a text[]
2018-10-09 16:32 SELECT UNION into a text[] Alexander Farber <alexander.farber@gmail.com>
@ 2018-10-09 16:39 ` David G. Johnston <david.g.johnston@gmail.com>
2018-10-10 08:29 ` Re: SELECT UNION into a text[] Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 1 reply; 3+ messages in thread
From: David G. Johnston @ 2018-10-09 16:39 UTC (permalink / raw)
To: Alexander Farber <alexander.farber@gmail.com>; +Cc: pgsql-general
On Tue, Oct 9, 2018 at 9:32 AM Alexander Farber <alexander.farber@gmail.com>
wrote:
>
> SELECT ARRAY_AGG(hashed)
> FROM words_nouns
> WHERE added > TO_TIMESTAMP(1539100913)
> UNION
> SELECT ARRAY_AGG(hashed)
> FROM words_verbs
> WHERE added > TO_TIMESTAMP(1539100913);
>
>
SELECT array_agg(words) FROM (
SELECT hashed FROM words_nouns
UNION ALL
SELECT hashed FROM words_verbs
) AS src
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: SELECT UNION into a text[]
2018-10-09 16:32 SELECT UNION into a text[] Alexander Farber <alexander.farber@gmail.com>
2018-10-09 16:39 ` Re: SELECT UNION into a text[] David G. Johnston <david.g.johnston@gmail.com>
@ 2018-10-10 08:29 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 0 replies; 3+ messages in thread
From: Andrew Gierth @ 2018-10-10 08:29 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Alexander Farber <alexander.farber@gmail.com>; pgsql-general
>>>>> "David" == David G Johnston <david.g.johnston@gmail.com> writes:
>> SELECT ARRAY_AGG(hashed)
>> FROM words_nouns
>> WHERE added > TO_TIMESTAMP(1539100913)
>> UNION
>> SELECT ARRAY_AGG(hashed)
>> FROM words_verbs
>> WHERE added > TO_TIMESTAMP(1539100913);
David> SELECT array_agg(words) FROM (
David> SELECT hashed FROM words_nouns
David> UNION ALL
David> SELECT hashed FROM words_verbs
David> ) AS src
Or even better,
SELECT ARRAY(SELECT hashed FROM words_nouns
UNION
SELECT hashed FROM words_verbs);
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-10-10 08:29 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-10-09 16:32 SELECT UNION into a text[] Alexander Farber <alexander.farber@gmail.com>
2018-10-09 16:39 ` David G. Johnston <david.g.johnston@gmail.com>
2018-10-10 08:29 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
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