Author |
Topic |
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 03:30:40
|
table data :a b c1 2 31 2 41 2 51 9 31 9 5output shud be a b c1 2 31 9 3Top1 of a,b key column |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-10-31 : 03:54:51
|
select a, b, min(c)from urTablegroup by a, bEm |
 |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 03:57:17
|
thank u ..i got the answer.. smart thinking ... |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 04:37:19
|
hi elancaster ..if i have n number of varchar and integer coulmn mean also i need to select all columns .. a and b is only key.. |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-10-31 : 04:41:40
|
can you give an example with more sample data? i'm not clear from your question whether you want to include the other columns in your 'group by' or ignore them or get a 'min' value againEm |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-31 : 04:42:20
|
quote: Originally posted by niranjankumark hi elancaster ..if i have n number of varchar and integer coulmn mean also i need to select all columns .. a and b is only key..
It depends.You need to post some sample data with expected resultMadhivananFailing to plan is Planning to fail |
 |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 04:49:53
|
a b c d .......1 2 dr hi1 2 fr hello1 2 gr hy1 9 gr ht1 9 bg trAssume there is no integer column ...a & b is only key column...top 1 shud taken ....a b c d 1 2 dr hi1 9 gr ht |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-10-31 : 04:54:57
|
you can still use the min function on a string. the question really is whether that should genuinely be the basis for selection? i.e. do you want the first one alphabetically or is there some other criteria you've not explained?Em |
 |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 05:04:25
|
I need only top 1 of group by a,b. if i put min of in varchar not getting actual result.. like integer |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-10-31 : 05:09:03
|
but the question is what do you mean by top? the first alphabetically? the first by some id column? the first record based on 1 column or a combination of columns? etc...you can just group by a and b and put a min function around your other columns but it's not really clear from you description whether that would give you the desired result or notEm |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-31 : 05:09:29
|
quote: Originally posted by niranjankumark I need only top 1 of group by a,b. if i put min of in varchar not getting actual result.. like integer
TOP 1 without ORDER BY has no meaning. You said you wanted TOP 1 GROUP by a, b but what is the order ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 05:21:38
|
a b c d e 1 2 dr hi 31 2 fr he 41 2 gr hy 31 9 gr ht 21 9 bg tr 4Assume there is no integer column ...a & b is only key column...top 1 shud taken ....a b c d e 1 2 dr hi 31 9 gr ht 2select a,b,c,e,min(d) from tablegroup by a,b,c,ehere group by shud have all select column except min() column.so expected result cant come.. my point take column a ,b .. with distinct value ...ex :a b 1 21 9 mean with this top 1 of 1,2 row(single)and top 1 of 1,9 row ( single)I need to select all column.. so the problem comes.. |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-10-31 : 05:27:13
|
i was thinking more like...declare @table table (a int, b int, c varchar(10), d varchar(10))insert into @tableselect 1,2, 'dr', 'hi'union all select 1, 2, 'fr', 'hello'union all select 1, 2, 'gr', 'hy'union all select 1, 9, 'gr', 'ht'union all select 1, 9, 'bg', 'tr'select a,b,min(c),min(d)from @tablegroup by a,b but the question is whether a simple min function will meet your requirement or whether there is a more 'contrived' method for selecting which record to show?Em |
 |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 05:35:12
|
hi elancaster ...by query the output is right .. but i have more than 100 columns ...so min() has to put in all.. it will decrease performance ??? |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-10-31 : 05:43:59
|
decrease performance compared to what? i'm still not sure that it's actually what you want. is there not perhaps just a few of those 100 columns that contain the value you want the 'min' for? and then just select the rest of the columns based on that? i.e.declare @table table (a int, b int, c varchar(10), d varchar(10),e int)insert into @tableselect 1,2, 'dr', 'hi',10union all select 1, 2, 'fr', 'hello',9union all select 1, 2, 'gr', 'hy',15union all select 1, 9, 'gr', 'ht',6union all select 1, 9, 'bg', 'tr',5select t1.* from @table t1 join (select a,b,min(e) as efrom @tablegroup by a,b) t2on t1.a = t2.a and t1.b=t2.b and t1.e=t2.e Em |
 |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2007-10-31 : 07:16:53
|
yes enclaster this is what i need....thank a lot ... |
 |
|
|