| Author |
Topic |
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 00:01:52
|
| Can I select the min value without using cursor?TableCat, valueA, 1B, 1A, 0C, 3B, 2Expect outputCat, valueA, 0B, 1C, 3 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2011-05-19 : 00:04:43
|
| select Cat, min(value)from YourTablegroup by Cat; |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 00:06:21
|
Thank.quote: Originally posted by nathans select Cat, min(value)from YourTablegroup by Cat;
|
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2011-05-19 : 00:09:25
|
| no problemNathan Skerl |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 00:12:55
|
| Hi nathans,Can I insert the select statement in a "case when"?Because I want the output as followingCat, Value, CountA, 1, 0B, 1, 1A, 0, 1C, 3, 1B, 2, 0Thank |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-05-19 : 00:46:44
|
| Hey What is count in above output?In Love... With Me! |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 00:50:15
|
| If it is min, then the new column count should be display 1else 0Thank |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-05-19 : 01:04:52
|
| Try thisCreate Table #t (Cat nvarchar(2),Value int)Insert into #tSelect 'A', 1 union allSelect 'B', 1 union allSelect 'A', 0 union allSelect 'C', 3 Union allSelect 'B', 2 With Cte as(Select Cat,value,row=row_number() over (partition by Cat order by value) From #t)Select Cat,Value,Case When row=1 then 1 else 0 end as count from Cte order by CountIn Love... With Me! |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 01:54:21
|
| Hi raghuveer125,Thank you very much.I will try it, but I need more time.Thank |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 04:36:50
|
| Hi raghuveer125,Actually, I am using Sybase ase 1.5 which is not support "partition by ".Do you have any suggestion to solve it in other way?Thank very much |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-05-19 : 05:16:41
|
This is a MS SQL forum.You can get better help in a Sybase forum. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 21:21:49
|
| ok, thank |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-19 : 22:44:05
|
| Hi nathans, Actually, you and raghuveer125 already gave me the idea how to solve it.I use thisselect Cat, min(value) as min_value into #tmpfrom YourTablegroup by Cat;to create a temp table as min value tablethen I use a case when to check it whether in the table case when ( exists ( select * from #tmp where YourTable.Cat= #tmp.Cat and YourTable.value = #tmp.min_value ) then 1 else 0 end as count_valueI am think if there is a better way to solve it, do you have any idea? |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-05-20 : 02:07:47
|
| Follow webfred, go to http://www.dbforums.com/sybase/ for help with sybase.But this is last try if not work then go for http://www.dbforums.com/sybase/ and post your queryCreate Table #t (Cat nvarchar(2),Value int)Insert into #tSelect 'A', 1 union allSelect 'B', 1 union allSelect 'A', 0 union allSelect 'C', 3 Union allSelect 'B', 2Select T.Cat,T.Value,Case When T.Cat=TT.Cat Then TT.min Else T.min End from ((Select *,0 As min From #t) as T Left outer join (Select Cat,Min(Value) As Value,1 As min From #t Group by CAt) as tt On T.CAt=TT.cat and T.value=tt.value)In Love... With Me! |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-05-20 : 03:50:07
|
| Hi raghuveer125,Your solution is much better, I will try it.Thank you. |
 |
|
|
|