Reply to Re: SQL query

Your name:

Reply:


Posted by frank m via SQLMonster.com on 07/13/05 11:05

Thanks for your reply. I tried this one:

select id from tbl
where
id in
(select id from tbl where cat='A')
and
id in
(select id from tbl where cat='B')

Which one do you think is more efficient? In reality I have 1 million records
and there are probably up to 10 CAT values.

Thanks


Erland Sommarskog wrote:
>> I can't make my mind up what the most efficient query is for the following
>> problem. My table contains records similar to this example:
>[quoted text clipped - 6 lines]
>> I now want to extract all ids that have the CAT value 'A' and 'B' .
>> In this example the result would be 1,3.
>
>SELECT DISTINCT id
>FROM tbl a
>WHERE EXISTS (SELECT *
> FROM tbl b
> WHERE a.id = b.id
> AND b.CAT = 'A')
> AND EXISTS (SELECT *
> FROM tbl b
> WHERE a.id = b.id
> AND b.CAT = 'B')
>


--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-general/200507/1

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация