| Author |
Topic |
|
ravivyas.rv
Starting Member
5 Posts |
Posted - 2009-10-05 : 12:39:46
|
| ble/ view : table1no. country name2 USA ra3 USA aa4 ITALY vv15 INDIA abc1 INDIA vir3 ENGLAND eng5 ENGLAND ngNow I want the result by grouping country with max(no) as either the results asno. country name3 USA aa4 ITALY vv15 INDIA abc5 ENGLAND ngorcountry nameUSA aaITALY vvINDIA abcENGLAND ngpls help me out...what could be the query... |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-05 : 13:02:05
|
Here's one way:select max(ca.[no]) as [no] ,t1.country ,max(ca.[name]) as namefrom table1 t1cross apply ( select top 1 [no], [name] from table1 where country = t1.country order by [no] desc ) cagroup by t1.country EDIT:this assumes you are using MS Sql Server version 2005 or laterBe One with the OptimizerTG |
 |
|
|
ravivyas.rv
Starting Member
5 Posts |
Posted - 2009-10-05 : 13:25:37
|
| No Its not working...And am using oracle sql plus...and m repeating my query as there was some display probtable/ view : table1no. / country / name2 / USA / ra3 / USA / aa4 / ITALY / vv15 / INDIA / abc1 / INDIA / vir3 / ENGLAND / eng5 / ENGLAND / ngNow I want the result by grouping country displaying only max(no): either the results asno. / country / name3 / USA / aa4 / ITALY / vv15 / INDIA / abc5 / ENGLAND / ngor(preferred)country / nameUSA / aaITALY / vvINDIA / abcENGLAND / ng |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-05 : 13:41:34
|
quote: Originally posted by ravivyas.rv No Its not working...And am using oracle sql plus...
Well......This is a sql server forum.....But this should work...as it's ANSI..well except for the table variableDECLARE @t99 table([no.] int, country varchar(10), [name] varchar(10))INSERT INTO @t99([no.], country, [name])SELECT 2, 'USA', 'ra' UNION ALLSELECT 3, 'USA', 'aa' UNION ALLSELECT 4, 'ITALY', 'vv' UNION ALLSELECT 15, 'INDIA', 'abc' UNION ALLSELECT 1, 'INDIA', 'vir' UNION ALLSELECT 3, 'ENGLAND', 'eng' UNION ALLSELECT 5, 'ENGLAND', 'ng'SELECT * FROM @t99 o WHERE EXISTS ( SELECT country, MAX(i.[no.]) FROM @t99 i WHERE i.country = o.country GROUP BY country HAVING MAX(i.[no.]) = o.[no.]) Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
gvphubli
Yak Posting Veteran
54 Posts |
Posted - 2009-10-05 : 13:43:39
|
| Use any of the aggrigate functions like min, max (which ever is appropriate in your case) on all the columns that you want to specify in the SELECT list.TechnologyYogihttp://gvphubli.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
ravivyas.rv
Starting Member
5 Posts |
Posted - 2009-10-05 : 13:55:06
|
| hey thanx brett... its working.... :)sorry for posting it in wrong form..I have one more doubt if I only want to print country and name only and not the no. then wat could be the query...thanx in advance...rv |
 |
|
|
ravivyas.rv
Starting Member
5 Posts |
Posted - 2009-10-05 : 13:56:45
|
| hey i got it...!!! It would be fine if you tell a bit of its workingrv |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-05 : 14:02:48
|
quote: Originally posted by ravivyas.rv hey i got it...!!! It would be fine if you tell a bit of its workingrv
the subquery gives you largest no value for each group of country. the outer query retrieves details of records with this no values. |
 |
|
|
ravivyas.rv
Starting Member
5 Posts |
Posted - 2009-10-05 : 14:12:49
|
| yepp brett.. I exacttly did the same thing.. thanx again...and thanx visakh for the explanation... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|