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 |
nfam
Starting Member
3 Posts |
Posted - 2011-01-17 : 01:29:16
|
hi,i have a query to get maximum value with group by funtionSELECT surveyID, questionID, choice, question, MAX(percentage) AS percentageFROM dbo.vw_surveyAnsMatrixPercMaxWHERE (question LIKE 'workload')GROUP BY surveyID, questionID, question, choicethe result of above query:surveyID questionID choice question percentage1 21 1 Workload 50.0000001 21 2 Workload 33.3333331 21 3 Workload 16.6666661 47 1 Workload 100.000000but i only want to get the highest data per group.and i don't want to group by choice, but still want to display choice data. so that the result be:surveyID questionID choice question percentage1 21 1 Workload 50.0000001 47 1 Workload 100.000000is it possible?thanks in advance.. |
|
Sachin.Nand
2937 Posts |
Posted - 2011-01-17 : 05:10:21
|
quote: i don't want to group by choice.is it possible?
Yesquote: i don't want to group by choice
Why ? |
|
|
nfam
Starting Member
3 Posts |
Posted - 2011-01-17 : 05:17:31
|
may i know how?the problem is, if don't put 'choice' in GROUP BY clause,it will give error:Sql Execution Error.Error message: column 'dbo.vw_surveyAnsMatrixPerc.choice' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.I want to display choice, but not group by choicethanks in advance |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-01-17 : 08:33:49
|
[code]select * from( select surveyID, questionID, choice, question, row_number()over(partition by percentage order by questionID )rid from dbo.vw_surveyAnsMatrixPercMax)t where rid=1[/code]PBUH |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-17 : 08:56:49
|
quote: Originally posted by Sachin.Nand
select * from( select surveyID, questionID, choice, question, row_number()over(partition by surveyid, questionID order by percentage desc)rid from dbo.vw_surveyAnsMatrixPercMax)t where rid=1 PBUH
N 56°04'39.26"E 12°55'05.63" |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-01-17 : 23:46:24
|
Thanks for the correction Peso.I had totally screwed it up.PBUH |
|
|
nfam
Starting Member
3 Posts |
Posted - 2011-01-18 : 00:07:14
|
thanks for your reply, unfortunately it hasn't resolve my problem.sorry if i didn't explain clear enough.say this is my data:surveyID questionID question percentage choice1 21 Access to company training 16.666666 21 21 Access to company training 16.666666 31 21 Access to company training 66.666666 11 21 Benefits 16.666666 21 21 Benefits 16.666666 31 21 Benefits 66.666666 11 21 Career advancement 16.666666 31 21 Career advancement 33.333333 21 21 Career advancement 50.000000 11 21 Pay and performance 16.666666 31 21 Pay and performance 33.333333 21 21 Pay and performance 50.000000 11 21 Flexibility of work hours 16.666666 21 21 Flexibility of work hours 16.666666 31 21 Flexibility of work hours 66.666666 11 21 Job security 16.666666 21 21 Job security 33.333333 31 21 Job security 50.000000 11 21 Materials and equiment 16.666666 21 21 Materials and equiment 33.333333 31 21 Materials and equiment 50.000000 11 21 Opportunity for advancement 16.666666 21 21 Opportunity for advancement 16.666666 31 21 Opportunity for advancement 66.666666 11 21 Growth opportunities 16.666666 31 21 Growth opportunities 33.333333 21 21 Growth opportunities 50.000000 11 21 Salary 16.666666 21 21 Salary 33.333333 31 21 Salary 50.000000 11 21 Workload 16.666666 31 21 Workload 33.333333 21 21 Workload 50.000000 11 22 Ability to make decision 33.333333 31 22 Ability to make decision 66.666666 11 22 Comfortable work environment 50.000000 11 22 Comfortable work environment 50.000000 31 22 Employee recognition program 16.666666 21 22 Employee recognition program 33.333333 31 22 Employee recognition program 50.000000 11 22 Employee training program 33.333333 31 22 Employee training program 66.666666 1from this data, i just want to get row with highest percentage per question.ie:- for question 21, suppose to get:1 21 Access to company training 66.666666 11 21 Benefits 66.666666 11 21 Career advancement 50.000000 11 21 Pay and performance 50.000000 11 21 Pay and performance 50.000000 11 21 Flexibility of work hours 66.666666 11 21 Job security 50.000000 11 21 Materials and equiment 50.000000 11 21 Opportunity for advancement 66.666666 11 21 Growth opportunities 50.000000 11 21 Salary 50.000000 11 21 Workload 50.000000 1for question 22:1 22 Ability to make decision 66.666666 11 22 Comfortable work environment 50.000000 11 22 Comfortable work environment 50.000000 31 22 Employee recognition program 50.000000 11 22 Employee training program 66.666666 1thanks.. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-18 : 02:16:09
|
Add your column Question to the PARTITION BY part in the windowed function. N 56°04'39.26"E 12°55'05.63" |
|
|
|
|
|
|
|