Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Need more clever solution for a selection
Thu, Mar 28 2013 8:23 AMPermanent Link

marcioehrlich

I need to select from a table where some records may be in two different groups. For instance, in this list, I need to select only those names who are exclusive in the particular group "A".

Grp   Name
A   John
B   Bob
A   Bob
A   Peter
B   James

So, I would only get John and Peter, since Bob belongs to "A" and "B".

I've tried this but it looked a little bit too much having to select twice the table:

SELECT Name FROM TableList
WHERE Grp = 'A'
 AND Name NOT IN (SELECT Name FROM TableList WHERE Grp <> 'A')

Can anyone suggest me a better solution?

Tks,

Marcio Ehrlich
Fri, Mar 29 2013 1:07 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Marcio,

<< I need to select from a table where some records may be in two different
groups. For instance, in this list, I need to select only those names who
are exclusive in the particular group "A". >>

You could also use EXCEPT:

SELECT Name FROM TableList
WHERE Grp = 'A'
EXCEPT
SELECT Name FROM TableList
WHERE Grp <> 'A'

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Mar 29 2013 1:18 PMPermanent Link

marcioehrlich

It looks like there is no way of avoiding both selections. I'll try to learn more about "EXCEPT".

Thank you, Tim.

M.

"Tim Young [Elevate Software]" wrote:

SELECT Name FROM TableList
WHERE Grp = 'A'
EXCEPT
SELECT Name FROM TableList
WHERE Grp <> 'A'
Mon, Apr 1 2013 5:08 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Marcio,

<< It looks like there is no way of avoiding both selections. I'll try to
learn more about "EXCEPT". >>

No, you can't avoid that.  It's really a matter of which syntax you prefer.

Tim Young
Elevate Software
www.elevatesoft.com
Image