Search for lists

Lists: pgsql-general
From: Daron Ryan <daron(dot)ryan(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Search for lists
Date: 2011-06-18 21:51:37
Message-ID: 4DFD1DE9.8040106@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello,

I need to search a table to find sets of rows that have a column
matching itself for the whole set and another column matching row for
row with a list I am going to supply. The result I should receive should
be value of the column that matches itself.

For example given the following data in my table:

3; 1
3; 2
4; 8
4; 9
4; 10

I might need to search for 1,2. This should produce the result 3. Or if
I were to search for 8, 9, 10 the result should be 4. Searching for 8, 9
should produce an empty result as should 8, 9, 10, 11.

Can anyone recommend a strategy?


From: David Johnston <polobo(at)yahoo(dot)com>
To: Daron Ryan <daron(dot)ryan(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Search for lists
Date: 2011-06-18 22:14:40
Message-ID: 347678D0-92D2-46C9-942D-27651A72D57B@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Untested approach
Use array_agg on column 2 along with group by on column 1 to build check arrays and then use equals to compare with an array of your desired input values.

You should omit duplicates and order ascending both the data and the input to ensure you are matching canonical forms.

David J.

On Jun 18, 2011, at 17:51, Daron Ryan <daron(dot)ryan(at)gmail(dot)com> wrote:

> Hello,
>
> I need to search a table to find sets of rows that have a column matching itself for the whole set and another column matching row for row with a list I am going to supply. The result I should receive should be value of the column that matches itself.
>
> For example given the following data in my table:
>
> 3; 1
> 3; 2
> 4; 8
> 4; 9
> 4; 10
>
> I might need to search for 1,2. This should produce the result 3. Or if I were to search for 8, 9, 10 the result should be 4. Searching for 8, 9 should produce an empty result as should 8, 9, 10, 11.
>
> Can anyone recommend a strategy?
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general


From: David Johnston <polobo(at)yahoo(dot)com>
To: Daron Ryan <daron(dot)ryan(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Search for lists
Date: 2011-06-18 22:39:48
Message-ID: 2CE9F932-85E0-44BC-82D2-A01F98DDB575@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

An alternative approach would be to select using a IN condition on the where clause and group by column 1 and column 2. Then, using this as a sub-select group by the resultant column 1 and a count on column two. The matching identifiers are those with a count equal to the number of entries in the original IN condition.

Basically count how many of values each distinct key in column 1 matches and keep those keys where the count and the number of values match.

David J.

On Jun 18, 2011, at 17:51, Daron Ryan <daron(dot)ryan(at)gmail(dot)com> wrote:

> Hello,
>
> I need to search a table to find sets of rows that have a column matching itself for the whole set and another column matching row for row with a list I am going to supply. The result I should receive should be value of the column that matches itself.
>
> For example given the following data in my table:
>
> 3; 1
> 3; 2
> 4; 8
> 4; 9
> 4; 10
>
> I might need to search for 1,2. This should produce the result 3. Or if I were to search for 8, 9, 10 the result should be 4. Searching for 8, 9 should produce an empty result as should 8, 9, 10, 11.
>
> Can anyone recommend a strategy?
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general


From: Daron Ryan <daron(dot)ryan(at)gmail(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Search for lists
Date: 2011-06-19 05:08:31
Message-ID: 4DFD844F.2030901@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thanks.
On 19/06/2011 8:09 AM, David Johnston wrote:
> An alternative approach would be to select using a IN condition on the where clause and group by column 1 and column 2. Then, using this as a sub-select group by the resultant column 1 and a count on column two. The matching identifiers are those with a count equal to the number of entries in the original IN condition.
>
> Basically count how many of values each distinct key in column 1 matches and keep those keys where the count and the number of values match.
>
> David J.
>
>
> On Jun 18, 2011, at 17:51, Daron Ryan<daron(dot)ryan(at)gmail(dot)com> wrote:
>
>> Hello,
>>
>> I need to search a table to find sets of rows that have a column matching itself for the whole set and another column matching row for row with a list I am going to supply. The result I should receive should be value of the column that matches itself.
>>
>> For example given the following data in my table:
>>
>> 3; 1
>> 3; 2
>> 4; 8
>> 4; 9
>> 4; 10
>>
>> I might need to search for 1,2. This should produce the result 3. Or if I were to search for 8, 9, 10 the result should be 4. Searching for 8, 9 should produce an empty result as should 8, 9, 10, 11.
>>
>> Can anyone recommend a strategy?
>>
>> --
>> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-general


From: "F(dot) BROUARD / SQLpro" <sqlpro(at)club-internet(dot)fr>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Search for lists
Date: 2011-06-19 19:05:54
Message-ID: 4DFE4892.1090004@club-internet.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Le 18/06/2011 23:51, Daron Ryan a écrit :
>
> Hello,
>
> I need to search a table to find sets of rows that have a column
> matching itself for the whole set and another column matching row for
> row with a list I am going to supply. The result I should receive should
> be value of the column that matches itself.
>
> For example given the following data in my table:
>
> 3; 1
> 3; 2
> 4; 8
> 4; 9
> 4; 10
>
> I might need to search for 1,2. This should produce the result 3. Or if
> I were to search for 8, 9, 10 the result should be 4. Searching for 8, 9
> should produce an empty result as should 8, 9, 10, 11.
>
> Can anyone recommend a strategy?
>

this is a general case of relationnal division.

One way to do this is :

WITH
T0 AS
(SELECT 1 AS N --> all the data to be search each on a separate SELECT
UNION ALL --> with UNION ALL
SELECT 2 AS N)
SELECT TBL_ID
FROM T_MY_TABLE_TBL AS T
INNER JOIN T0
ON T.TBL_VALUE = T0.N
GROUP BY TBL_ID
HAVING COUNT(*) = (SELECT COUNT(*)
FROM T0);

I you read french, I wrote a paper on the relational division :

http://sqlpro.developpez.com/cours/divrelationnelle/

A +

--
Frédéric BROUARD - expert SGBDR et SQL - MVP SQL Server - 06 11 86 40 66
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Enseignant Arts & Métiers PACA, ISEN Toulon et CESI/EXIA Aix en Provence
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
*********************** http://www.sqlspot.com *************************