| Author |
Topic |
|
Jusvistin
Yak Posting Veteran
81 Posts |
Posted - 2003-08-07 : 11:34:40
|
| Hello, this is my first post in this forum.The following query returns the countryname with the largest area for each region. Is there a way to accomplish this same result by joiningthe table to itself ?-----------------------------------------------------select a.region, a.countryname, a.areafrom cia awhere a.area >= all(select areafrom cia bwhere a.region = b.region)-----------------------------------------------------Thanks,Kevin |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-08-07 : 11:43:55
|
quote: Originally posted by Jusvistin Hello, this is my first post in this forum.The following query returns the countryname with the largest area for each region. Is there a way to accomplish this same result by joiningthe table to itself ?-----------------------------------------------------select a.region, a.countryname, a.areafrom cia awhere a.area >= all(select areafrom cia bwhere a.region = b.region)-----------------------------------------------------Thanks,Kevin
First, Welcome...Second, I'm sureThird, ALL is not a SQL syntaxFourth, you're basically self joing to the same table...but for what purpose.....what does (correct syntax) SELECT a.region, a.countryname, a.area FROM cia a WHERE a.area IN ( SELECT area FROM cia b WHERE a.region = b.region ) Do for you...I would imagine that is the same asSELECT a.region, a.countryname, a.area FROM ciaNo?What is the result set you want to get?Brett8-)SELECT POST=NewId() |
 |
|
|
vganesh76
Yak Posting Veteran
64 Posts |
Posted - 2003-08-07 : 11:44:02
|
| Jusvistin, I find a problem with ur query, bcos u r matching the same table with no other additional condition.any way u can try this, But I assume that this is what ur requirement is. If not please do explain in detail.select a.region, a.countryname, max(a.area)from cia agroup by a.region,a.countrynameV.GaneshNetAssetManagement.Comvganesh76@rediffmail.comEnjoy working |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2003-08-07 : 11:46:11
|
| This accomplishes what you want but is not exactly a self-joinselect a.region, a.countryname, a.areafrom cia a join(select region, max(area) 'area'from ciagroup by region) b on a.region = b.region and a.area = b.area |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-08-07 : 11:48:36
|
| the subquery could beselect a.region, a.countryname, a.areafrom cia awhere a.area =(select max(area)from cia bwhere a.region = b.region)alsoselect a.region, a.countryname, a.areafrom cia a join (select region, area = max(area) from cia group by region) bon a.region = b.regionand a.area = b.area==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2003-08-07 : 11:50:58
|
| X002548 - Nope, you've got it wrong there. ALL is valid sql syntax, so is ANY and SOME. They are rarely used though. |
 |
|
|
Jusvistin
Yak Posting Veteran
81 Posts |
Posted - 2003-08-07 : 12:04:39
|
| Hello,The query I'm using works fine, just seems to me I should be able to accomplish the same thing with a self-join.The CIA table has a single row for each country.Here is the table structure:Name - Country nameRegion - Approximates a continentArea - Area in square miles.My query returns a result set that contains a single row for each region containing the country with the largest area.Results look like this:--------------------------------------------------------------------Africa Sudan 2505810Antarctic Region Antarctica 14000000Arctic Region Greenland 2175600Asia Russia 17075200Bosnia and Herzegovina, Europe Bosnia and Herzegovina 51129Central America and the Caribbean Nicaragua 129494Commonwealth of Independent States Kazakhstan 2717300Europe Turkey 780580Middle East Saudi Arabia 1960582North America Canada 9976140Oceania Australia 7686850South America Brazil 8511965South America, Central America and the Caribbean Colombia 1138910Southeast Asia Indonesia 1919440World Cape Verde 4033-------------------------------------------------------------------Thanks,Kevin |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-08-07 : 12:36:48
|
quote: Originally posted by setbasedisthetruepath X002548 - Nope, you've got it wrong there. ALL is valid sql syntax, so is ANY and SOME. They are rarely used though.
Damn, I hate when that happens...quote: ALLCompares a scalar value with a single-column set of values. Syntaxscalar_expression { = | <> | != | > | >= | !> | < | <= | !< } ALL ( subquery )Argumentsscalar_expressionIs any valid Microsoft® SQL Server™ expression.{ = | <> | != | > | >= | !> | < | <= | !< }Is a comparison operator.subqueryIs a subquery that returns a result set of one column. The data type of the returned column must be the same data type as the data type of scalar_expression.Is a restricted SELECT statement (the ORDER BY clause, the COMPUTE clause, and the INTO keyword are not allowed).Return TypesBoolean
Kevin, did you see Nigels response? That's a join...Brett8-)SELECT POST=NewId() |
 |
|
|
Jusvistin
Yak Posting Veteran
81 Posts |
Posted - 2003-08-07 : 12:50:07
|
| Thanks everybody,You guys are fast, AND good.I now know where to come with all my newbie questions.Kevin |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2003-08-07 : 12:54:59
|
quote: Originally posted by Jusvistin Thanks everybody,You guys are fast, AND good.I now know where to come with all my newbie questions.Kevin
Hmmm, too fast for my liking really. I wish I could have the pleasure of being the first to give an answer! Oh well.... ----------------Shadow to Light |
 |
|
|
|