| Author |
Topic |
|
Finarfin
Starting Member
28 Posts |
Posted - 2004-06-01 : 06:10:36
|
Hi all,I have a problem to create my query, may you could help me.There is one table:table1 : code, country1 code can have several countriesI want to select * from table1 where code is only associated to ONE country.This must be something like that:select distinct code, country from table1where(count(select distinct country from table1 where code = code)=1)...but it is of course not Thank you all |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2004-06-01 : 06:27:51
|
| [code]select * from yourTablewhere exists (select 1 from yourTable as a where a.code= yourTable.code group by code having count(*) = 1)[/code]_________________________________________________________The cradle of civilisation will defeat the new monsters of the world. |
 |
|
|
Wanderer
Master Smack Fu Yak Hacker
1168 Posts |
Posted - 2004-06-01 : 06:30:04
|
| try :select code,country,count(*)from table1group by code,countryhaving count(*) = 1*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here! |
 |
|
|
Wanderer
Master Smack Fu Yak Hacker
1168 Posts |
Posted - 2004-06-01 : 06:31:19
|
| Hmm - go with Amethysiums - he post while I was typing, and is neater, it would seem*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here! |
 |
|
|
Finarfin
Starting Member
28 Posts |
Posted - 2004-06-01 : 06:43:20
|
| Thank you both of you,But the queries you sent me aren't working. I think I forgot some informations when asking the question.The table I have is like this:code country01 USA01 FRANCE02 USA03 GERMANY03 USA04 FRANCE05 ...... ...The query will return02 USA04 FRANCEThank you all |
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-06-01 : 06:48:43
|
| select max(code) as code, countryfrom yourtablegroup by countryDuane.ooops! after Wanderers analysis I realised that I was way off.I Totally misunderstood the question - I realised this once I read the entire thread as opposed to the last post. |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2004-06-01 : 07:21:57
|
| create table #moo (code int, con varchar(10))insert into #moo values (1,'usa')insert into #moo values (1,'france')insert into #moo values (2,'usa')insert into #moo values (3,'germany')insert into #moo values (3,'usa')insert into #moo values (4,'france')select max(code) as code, confrom #moogroup by conselect a.code, a.confrom #moo aleft outer join #moo bon a.code = b.codeand a.con != b.conwhere b.code is null-------Moo. :) |
 |
|
|
Wanderer
Master Smack Fu Yak Hacker
1168 Posts |
Posted - 2004-06-01 : 08:47:04
|
Well, I ran Ames's (sorry for the shortening) after Mist's temp create table, and it give the same result as Mist's:code con ----------- ---------- 2 usa4 francewhich certainly seems to be the solution, given the test case.It will differ in a real environment, but on a very small table (Mist's example), Ames's solution SEEMs marginally better from an access path point of view: 0.0755 cost vs 0.0756 Finarfin : it that doesn't help, please give the specific table structure - that might give us a clue.Cheers*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here! |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2004-06-01 : 08:52:32
|
Wanderer, thanks for the analysis. I've always encourged myself to use the EXISTS operator as it always seems to score on performance Vs other ways of doing the same thing.oh and nice to see you again Mr mist _________________________________________________________The cradle of civilisation will defeat the new monsters of the world. |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2004-06-01 : 09:04:46
|
| How would you know which country to return for code 01 and 03??? |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2004-06-01 : 09:08:25
|
| LOL, RickD I asked myself the same question but I guess he got the answer he's looking for. I'm sure his design deals with this though._________________________________________________________The cradle of civilisation will defeat the new monsters of the world. |
 |
|
|
Finarfin
Starting Member
28 Posts |
Posted - 2004-06-01 : 10:05:34
|
I will not go away without thanking you, that is just that I was in meeting...In fact, to answer your questions, I didn't gave you the whole table (there are 10 columns with a 4 columns primary key combination).I tried all the solutions you gave me and the only one working is mist's, I don't know why but Ame's one isn't giving me the good results with the whole table.But I don't think my problems are over because I need to add other conditions to that query...Be sure I will keep you in touch Thank you all |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2004-06-01 : 11:26:38
|
| Good luck Finarfin! say hi to Paris for me (if you live there).. haven't been for 2 years._________________________________________________________The cradle of civilisation will defeat the new monsters of the world. |
 |
|
|
Finarfin
Starting Member
28 Posts |
Posted - 2004-06-01 : 11:32:26
|
Thank you Ame, I will do my best!And I'll go to the Tour Eiffel to say hello to Paris for you Thank you all |
 |
|
|
|