Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Staryon
Starting Member
10 Posts |
Posted - 2003-01-10 : 16:22:17
|
| Hi!Here it goes my first post for the forum! Unfortunately is a problem that I have with a query :(I have the following table:FK_ID DATA1457 Spain1458 England1457 France1457 Spain1460 Portugal1457 Germany1458 Spain1460 FranceI need to retrieve that FK_ID which have the DATA='Spain' AND DATA='France'. For this example, we'll get the FK_ID = 1457The ugly solution is this one:select distinct FK_ID from MyTable where FK_ID in ( select FK_ID from MyTable where (Data='Spain') AND FK_ID in ( select FK_ID from MyTable where (Data='France') ) ) Do you guys how can I build a more optimized query that makes the same?Thanks a lotEdited by - Staryon on 01/10/2003 16:24:35 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-01-10 : 16:42:20
|
| This might be a little faster:SELECT A.FK_IDFROM myTable A INNER JOIN myTable BON A.FK_ID=B.FK_IDWHERE A.Data='Spain' AND B.Data='France' |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-01-10 : 17:02:14
|
| Not only is Robvolk's query probably faster, but it is also easier to read. |
 |
|
|
Staryon
Starting Member
10 Posts |
Posted - 2003-01-10 : 17:48:59
|
| Thanks for your answer, robvolkMy only concern is that I may have more than two conditions, therefore I could have many INNERJOINs ( or many SELECT ... with my solution ) for the same table, but I suppose that maybe there is not a better way to do this. Thanks a lot, I'll use it with my code. |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-01-10 : 18:08:15
|
| This might help with multiple conditions:SELECT FK_IDFROM myTableWHERE Data IN('France','Spain')GROUP BY FK_IDHAVING Count(DISTINCT Data)=2So if you wanted to add England:SELECT FK_IDFROM myTableWHERE Data IN('France','Spain','England')GROUP BY FK_IDHAVING Count(DISTINCT Data)=3It's a little more flexible, but the only way to truly make it automatic is to use two tables; the original and another containing only the values being searched for:CREATE TABLE #Countries (Data varchar(20) NOT NULL PRIMARY KEY)INSERT INTO #Countries VALUES ('Spain')INSERT INTO #Countries VALUES ('France')INSERT INTO #Countries VALUES ('England')SELECT A.FK_IDFROM myTable A INNER JOIN #Countries CON A.Data=C.DataGROUP BY A.FK_IDHAVING Count(DISTINCT A.Data)=(SELECT Count(DISTINCT Data) FROM #Countries)You'd have to weigh the effort in creating and populating the temp table with the values, against the (minimal) rewriting needed to accommodate more or fewer search items. I'm also inclined to think that the GROUP BY...HAVING technique may not perform as well as the self-join, unless you'll be searching for 5 or more items most of the time. It depends on how the table is indexed and how the data is distributed.Edited by - robvolk on 01/10/2003 18:11:06 |
 |
|
|
Staryon
Starting Member
10 Posts |
Posted - 2003-01-10 : 18:35:37
|
| Thanks a lot for that wonderful explanation, robvolkI will consider to use one of those examples.The only problem with your second option, is that I don't know a priori what countries (it was an example) I am going to use, so I cannot populate that table. I'll keep in mind that solution for the future.Take care |
 |
|
|
|
|
|
|
|