| Author |
Topic |
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 09:23:10
|
| In my table, I have a column whose value is repeated in several rows. I want only the row containing its highest value.For ex, my table ll be like this:Col1 Col21 11 21 32 72 93 13 53 13I want my query to return omly the rows having the haighest values of Col2 in each group. So the returned result should be:Col1 Col21 32 93 13What should be my query plsThanks |
|
|
Q
Yak Posting Veteran
76 Posts |
Posted - 2006-07-21 : 09:27:13
|
| select col1, max(col2)from tablegroup by col1 |
 |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 09:32:57
|
| u re the man thank you |
 |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 09:34:38
|
| and if we want all the columns of the table and not only col1 and col2 without specifyingselect max(col2), col1,col3,col43....., coln from ....how d that bethank you |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-21 : 09:37:10
|
From your expected result it seems like you want Query1. From your description, it sounds like the 2nd query.declare @table table( Col1 int, Col2 int)insert into @tableselect 1, 1 union allselect 1, 2 union allselect 1, 3 union allselect 2, 7 union allselect 2, 9 union allselect 3, 1 union allselect 3, 5 union allselect 3, 13-- Query 1select Col1, max(Col2) as max_Col2from @tablegroup by Col1order by Col1/* RESULTCol1 max_Col2 ----------- ----------- 1 32 93 13*/-- Query 2select Col1, Col2from @table twhere Col2 = (select max(Col2) from @table x where x.Col1 = t.Col1)order by Col1/* RESULTCol1 Col2 ----------- ----------- 1 32 93 13*/ KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-21 : 09:38:40
|
quote: Originally posted by rtutus and if we want all the columns of the table and not only col1 and col2 without specifyingselect max(col2), col1,col3,col43....., coln from ....how d that bethank you
That's the Query 2. KH |
 |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 09:39:45
|
| What if I want all the columns and not only Col1 and Col2 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-21 : 09:41:54
|
[code]declare @table table( Col1 int, Col2 int, Col3 int)insert into @tableselect 1, 1, 2 union allselect 1, 2, 3 union allselect 1, 3, 4 union allselect 2, 7, 5 union allselect 2, 9, 3 union allselect 3, 1, 2 union allselect 3, 5, 3 union allselect 3, 13,1select Col1, Col2, Col3from @table twhere Col2 = (select max(Col2) from @table x where x.Col1 = t.Col1)order by Col1/* RESULTCol1 Col2 Col3 ----------- ----------- ----------- 1 3 42 9 33 13 1*/[/code] KH |
 |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 10:25:15
|
| I have my table OrdHead already created. The field for which I want the maximum values is No_Ref and the field that s repeated is Sales_Ord. so I do this:select * from ordhead as t where t.no_ref=(select max(no_ref) from ordhead as x where t.no_ref=x.no_ref group by sales_ord) order by sales_ordBut I get duplicates in my Sales_ord field. That s not what I want.Thanks |
 |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 10:47:14
|
| or using Col1, Col2... as in above I do this:select * from MyTable as t where t.col2=(select max(col2) from MyTable as x where t.col2=x.col2 group by col1) order by col1but it s not giving me the expected result |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-21 : 10:51:32
|
Can you post your table structure, some sample data and result that you want ? KH |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-21 : 10:52:30
|
You've just not copied khtan's example. Try something like this....select * from ordhead twhere no_ref = (select max(no_ref) from ordhead x where x.sales_ord = t.sales_ord)order by sales_ord Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-07-21 : 10:58:27
|
| sorry my mistake your query was perfect. But I put Col2 instead of col1 in :select max(col2) from MyTable as x where t.col2=x.col2 Now I chnged Col2 with Col1 and it s working perfectlyThanks a lot |
 |
|
|
|