Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:48:32+00:00 2026-06-16T02:48:32+00:00

I’m not a specialist in SQL and I struggle to find a solution to

  • 0

I’m not a specialist in SQL and I struggle to find a solution to make a SQL query for the following case. I hope someone can help me with this one!

I have a few tables where the Answer table holds the answers from the 20 Questions table. Where the answer can have a value from 1 to 5.

The questions have a types_id that will mark the questions that are related.

What I need is a query over the answer table and get the following information:

Group the question that are related ( = same types_id = same teamid and = same date ) and take the AVG from the answers that have the same types_id.

So the result can be something like:

---------------------------------------------------------
|                                       types_id        |
|teamid | date                |   1  |  2  |   3   |  4 | 
---------------------------------------------------------
|    12 | 2012-12-31 00:00:00 |   2  |  4  |   3   |  5 | <- holds the average answers from the related questions ( = same types_id)
---------------------------------------------------------

As an example here the Questions 1, 5, 9, 13 and 17 are related by there types_is of 1. So there are 4 groups of related questions.

Below a sample of the table structures:

Answers table:

-----------------------------------------------------------------------------------------------------------------------------------------------------
id teamid   userid  date                    Q1  Q2  Q3  Q4  Q5  Q6  Q7  Q8  Q9  Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 timestamp           done
-----------------------------------------------------------------------------------------------------------------------------------------------------
1      12        1  2012-12-31 00:00:00     1   1   1   1   1   1   2   2   2   2   2   3   3   3   3   3   4   4   4   4   2012-12-11 08:30:27 0
2      12        2  2012-12-31 00:00:00     5   2   5   5   5   5   4   4   4   4   4   3   3   3   3   3   2   2   2   2   2012-12-11 08:50:08 0
3      12        3  2012-12-31 00:00:00     1   3   1   1   1   1   2   2   2   2   2   4   4   4   4   4   5   5   5   5   2012-12-11 08:20:37 0
1       9       11  2012-12-31 00:00:00     1   1   1   1   1   1   2   2   2   2   2   3   3   3   3   3   4   4   4   4   2012-12-11 08:30:27 0
2       9       12  2012-12-31 00:00:00     5   2   5   5   5   5   4   4   4   4   4   3   3   3   3   3   2   2   2   2   2012-12-11 08:50:08 0
3       9       23  2012-12-31 00:00:00     1   3   1   1   1   1   2   2   2   2   2   4   4   4   4   4   5   5   5   5   2012-12-11 08:20:37 0
    -----------------------------------------------------------------------------------------------------------------------------------------------------

Questions table

---------------------------------
id  question            types_id
---------------------------------
1   Question  1 text        1
2   Question  2 text        2
3   Question  3 text        3
4   Question  4 text        4
5   Question  5 text        1
6   Question  6 text        2
7   Question  7 text        3
8   Question  8 text        4
9   Question  9 text        1
10  Question 10 text        2
11  Question 11 text        3
12  Question 12 text        4
13  Question 13 text        1
14  Question 14 text        2
15  Question 15 text        3
16  Question 16 text        4
17  Question 17 text        1
18  Question 18 text        2
19  Question 19 text        3
20  Question 10 text        4
---------------------------------

Any help will be greatly appreciated!

Thanks Aren

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:48:33+00:00Added an answer on June 16, 2026 at 2:48 am

    First you need to unpivot the question data. I’d create view for this if you aren’t prepared to store the data this way. You’ll need to expand this to all 20 questions:

    Create View UnpivotedAnswers As
    Select
      teamid,
      date,
      1 as QuestionID,
      Q1 as Answer
    From
      Answers
    Union All
    Select
      teamid,
      date,
      2 as QuestionID,
      Q2 as Answer
    From
      Answers
    Union All
    Select
      teamid,
      date,
      5 as QuestionID,
      Q5 as Answer
    From
      Answers
    

    Once you have the data available in this format, getting the averages out can be done like so:

    Select
      u.teamid,
      u.date,
      avg(case When q.types_id = 1 Then Answer End) as type1,
      avg(case When q.types_id = 2 Then Answer End) as type2,
      avg(case When q.types_id = 3 Then Answer End) as type3,
      avg(case When q.types_id = 4 Then Answer End) as type4,
      avg(case When q.types_id = 5 Then Answer End) as type5
    From
      UnpivotedAnswers u
        Inner Join
      Questions q
        On u.QuestionID = q.id
    Group By
      u.teamid,
      u.date
    

    http://sqlfiddle.com/#!2/b1b718/1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I need a function that will clean a strings' special characters. I do NOT
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.