| Author |
Topic |
|
SCHEMA
Posting Yak Master
192 Posts |
Posted - 2009-08-28 : 12:42:25
|
| I have table ACOLA COLB1 11 21 32 12 22 3How do I get min and max value for Table A? i.e1 11 32 12 3 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-08-28 : 12:49:46
|
| SELECT COLA,MIN(COLB)FROM Table AGROUP BY COLAUNION ALLSELECT COLA,MAX(COLB) FROM Table AGROUP BY COLAJim |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-28 : 23:11:49
|
| Hi, This is also one methoddeclare @temp table ( a int, b int )insert into @temp select 1,77insert into @temp select 1,54insert into @temp select 1,3insert into @temp select 1,23insert into @temp select 2,1insert into @temp select 2,2insert into @temp select 2,3SELECT a,columnss from ( SELECT a,min(b) AS minb,max(b) AS maxb FROM @temp GROUP BY a) tUNPIVOT ( columnss FOR [columns] IN (minb,maxb)) tt |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-29 : 11:31:24
|
[code]select COLA, COLBfrom ( select COLA, COLB, min_row = row_number() over (partition by COLA order by COLB), max_row = row_number() over (partition by COLA order by COLB desc) from tableA ) swhere s.min_row = 1or s.max_row = 1[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-31 : 01:47:19
|
orselect t1.* from @temp as t1 inner join( select a ,min(b) as min_b, max(b) as max_b from @temp group by a) as t2on t1.a=t2.a and t1.b in (t2.min_b,t2.max_b) MadhivananFailing to plan is Planning to fail |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-31 : 06:37:05
|
or try this on:select * from( select a, b from @temp where b >= all (select b from @temp t where t.a=a) union all select a, b from @temp where b <= all (select b from @temp t where t.a=a))dgroup by a, border by a, b |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-31 : 06:42:35
|
quote: Originally posted by ms65g or try this on:select * from( select a, b from @temp where b >= all (select b from @temp t where t.a=a) union all select a, b from @temp where b <= all (select b from @temp t where t.a=a))dgroup by a, border by a, b
It doesn't give the expected resultMadhivananFailing to plan is Planning to fail |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-31 : 07:18:58
|
quote: Originally posted by madhivanan
quote: Originally posted by ms65g or try this on:select * from( select a, b from @temp where b >= all (select b from @temp t where t.a=a) union all select a, b from @temp where b <= all (select b from @temp t where t.a=a))dgroup by a, border by a, b
It doesn't give the expected resultMadhivananFailing to plan is Planning to fail
you are right.select * from( select d1.a, b=(select top 1 b from @temp t where b >= all(select b from @temp where a=d1.a) and t.a=d1.a) from (select distinct a from @temp) as d1 union all select d2.a, b=(select top 1 b from @temp t where b <= all(select b from @temp where a=d2.a) and t.a=d2.a) from (select distinct a from @temp) as d2)ddorder by a |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-31 : 08:26:37
|
quote: Originally posted by ms65g
quote: Originally posted by madhivanan
quote: Originally posted by ms65g or try this on:select * from( select a, b from @temp where b >= all (select b from @temp t where t.a=a) union all select a, b from @temp where b <= all (select b from @temp t where t.a=a))dgroup by a, border by a, b
It doesn't give the expected resultMadhivananFailing to plan is Planning to fail
you are right.select * from( select d1.a, b=(select top 1 b from @temp t where b >= all(select b from @temp where a=d1.a) and t.a=d1.a) from (select distinct a from @temp) as d1 union all select d2.a, b=(select top 1 b from @temp t where b <= all(select b from @temp where a=d2.a) and t.a=d2.a) from (select distinct a from @temp) as d2)ddorder by a
Well. Your query will take more time than other solutionsMadhivananFailing to plan is Planning to fail |
 |
|
|
|