doulu3399 2012-01-12 13:01
浏览 64
已采纳

AVG在同一列上由另一列中的值单个sql查询

I want to find AVG on same column by values in different column single sql query.

table - question_rating
review_id
question_id
rating
  • for each review there are multiple 16 questions and ratings.
  • question_id values are 1 to 16.
  • rating values are from 1 to 5.

I want to find avg of group of questions as

Desired output-

qustion_id     rating
-------------------------
g1               4.4
g2               3.7
g3               5.6

g1 is group of questions (1,3) g2 is group of questions (2,6) g3 is group of questions (7,8) ....g8

pseudo code-

 select (if(qustion=1 and question=3) as g1,
if(qustion=2 and question=6) as g2), ..
)avg(rating of respective group) from question_rating

I know it can be done by taking query separately but I want to know by single query.

Or any easy way to find such output by php,etc.

  • 写回答

2条回答 默认 最新

  • dongmiyi8220 2012-01-12 13:06
    关注

    You should make a table question_groups

    question_id  group_id
    q1          g1
    q2          g2
    q3          g1
    q4          g3
    q5          g3
    q6          g2
    

    And the select will be

    select group_id, avg(rating)
    from question_rating a
      join question_groups b on (a.question_id= b.question_id)
    group by group_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表