Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mahesh6ag
Starting Member
1 Post |
Posted - 2011-04-10 : 02:13:59
|
| Dear SQL ExpertsI am new to the SQL and need your help to solve my problemI have a following table named stress with 5 fileds, and 75 recordsI use the the following SQL to retutn the maximum stress ratio from ratio field, and group by the combination, this what I was looking for , but I am unable to get the other corresponding fields from the table,example the Category, Stress, Allowable for these filtered values SQL Statement : SELECT combination,max(ratio) FROM stress GROUP BY combinationResultCombination |RatioAmb to T1 0.7887792Amb to T2 0.061314866GR + Max P 0.76127881Max P 0.78806025TR:T1 to T2 0.85066134If I include any other column in the SELECT, it gives me error, exSELECT Category, combination,max(ratio) FROM stress GROUP BY combination = error and if I include column in groupby clause the results are entirely different, Please help me. I tried using Join, or subquery it dosn't help.RegardsMahesh SAMPLE TABLECombination |Category |Stress |Allowable |RatioMax P Hoop 1.49E+04 1.89E+04 7.88E-01GR + Max P Sustain 8.57E+03 1.89E+04 4.54E-01TR:T1 to T2 Expansion 5.78E+02 2.97E+04 1.95E-02Amb to T1 Expansion 5.36E+02 2.97E+04 1.80E-02Amb to T2 Expansion 4.21E+01 3.00E+04 1.40E-03Max P Hoop 1.49E+04 1.89E+04 7.88E-01GR + Max P Sustain 7.27E+03 1.89E+04 3.85E-01TR:T1 to T2 Expansion 2.93E+02 2.97E+04 9.85E-03Amb to T1 Expansion 2.72E+02 2.97E+04 9.13E-03Amb to T2 Expansion 2.13E+01 3.00E+04 7.10E-04Max P Hoop 1.49E+04 1.89E+04 7.88E-01GR + Max P Sustain 1.44E+04 1.89E+04 7.61E-01TR:T1 to T2 Expansion 1.16E+03 2.97E+04 3.92E-02Amb to T1 Expansion 1.08E+03 2.97E+04 3.63E-02Amb to T2 Expansion 8.47E+01 3.00E+04 2.82E-03Max P Hoop 1.49E+04 1.89E+04 7.88E-01GR + Max P Sustain 6.78E+03 1.89E+04 3.59E-01TR:T1 to T2 Expansion 7.09E+03 2.97E+04 2.39E-01Amb to T1 Expansion 6.58E+03 2.97E+04 2.21E-01Amb to T2 Expansion 5.16E+02 3.00E+04 1.72E-02Max P Hoop 1.49E+04 1.89E+04 7.88E-01GR + Max P Sustain 7.54E+03 1.89E+04 3.99E-01TR:T1 to T2 Expansion 1.86E+04 2.97E+04 6.25E-01Amb to T1 Expansion 1.72E+04 2.97E+04 5.80E-01Amb to T2 Expansion 1.35E+03 3.00E+04 4.51E-02 ] |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-04-10 : 05:55:51
|
In most cases, when you have an aggregation function (like max, sum, avg etc.) in the select list, you will need to include any other columns in your select in the group by clause.So if you want to add Category in the select list, you must include that in the group by as well. But then, the max value you get is NOT the max within a combination. It is the max within the combination AND Category.A solution for this, if you are on SQL 2005 or higher, is to use windowing function like this:select combination, max(ratio) over (partition by combination) as MaxRatio, Category, Stress --, any other columnsfrom stress |
 |
|
|
|
|
|
|
|