Re: Pattern match against array elements? - Mailing list pgsql-general

From Israel Brewster
Subject Re: Pattern match against array elements?
Date
Msg-id D2749FB0-53D2-4DB4-98AF-8B4568DDA870@ravnalaska.net
Whole thread Raw
In response to Re: Pattern match against array elements?  (dinesh kumar <dineshkumar02@gmail.com>)
List pgsql-general
In the general case, that might work, however in my actually use case the inner SQL query (and underlying table structure) is rather more complicated, making it so I really want the WHERE clause in an outside query, as in my (non-functional) example. Just to clarify, the actual structure of the query is more like this:

SELECT 
<bunch of columns>
FROM
(SELECT
<some columns>
(SELECT
array_agg(flightnum)
FROM legdetails
WHERE logid=logs.id) as flightnums --this is where the array comes from that I want to filter on.
FROM logs
<joins with other tables>
) s1
WHERE
<filter on flightnums beginning with here>
<possible other filters here>
ORDER BY <whatever>

So the query is noticeably different than the one I original gave, but the end result is the same: an array in an inner query, and trying to filter based on the contents of the array. Sorry if I confused the issue by trying to simplify the concept too much.
-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------



On Oct 12, 2015, at 10:14 AM, dinesh kumar <dineshkumar02@gmail.com> wrote:

On Mon, Oct 12, 2015 at 10:58 AM, Israel Brewster <israel@ravnalaska.net> wrote:
Is there any way to do a pattern match against the elements of an array in postgresql (9.4 if the version makes a difference)? I have a grouped query that, among other things, returns an array of values, like:

SELECT lognum, array_agg(flightnum) as flightnums FROM logs GROUP BY lognum;

Where the flightnum field is a varchar containing either a text string or a three-or-four digit number. Now say I want to select all logs that have a flight number starting with an '8' (so '800' or '8000' series flights). My first thought was to do something like this:

SELECT * FROM (SELECT lognum,array_agg(flightnum) as flightnums FROM logs GROUP BY lognum) s1 WHERE '8%' like ANY(flightnums);

But while this doesn't give an error, it also doesn't return any results. I'm guessing that this is because the wildcard is on the left of the operator, and needs to be on the right. Of course, turning it around to be:

WHERE ANY(flightnum) like '8%'

gives me a syntax error. So is there any way I can run this query such that I get any rows containing a flight number that starts with an 8 (or whatever)?

 
Are you looking for this ?
 
SELECT lognum array_agg(flightnum) FROM logs WHERE flightnum ~ '^8' GROUP BY lognum;
 
 
 
-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------



--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



--

Attachment

pgsql-general by date:

Previous
From: Jim Nasby
Date:
Subject: Re: Constraint allowing value up to 2 times but no more than 2 times
Next
From: Israel Brewster
Date:
Subject: Re: Pattern match against array elements?