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 7965653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:13:07+00:00 2026-06-04T06:13:07+00:00

I have some problems with writing a SQL query for MySQL. I have a

  • 0

I have some problems with writing a SQL query for MySQL. I have a table with the following structure:

mysql> select id, pass, val from data_r1 limit 10;
+------------+--------------+----------------+
| id         | pass         | val            |
+------------+--------------+----------------+
| DA02959106 | 5.0000000000 |  44.4007000000 |
| 08A5969201 | 1.0000000000 | 182.4100000000 |
| 08A5969201 | 2.0000000000 | 138.7880000000 |
| DA02882103 | 5.0000000000 |  44.7265000000 |
| DA02959106 | 1.0000000000 | 186.1470000000 |
| DA02959106 | 2.0000000000 | 148.2660000000 |
| DA02959106 | 3.0000000000 | 111.9050000000 |
| DA02959106 | 4.0000000000 |  76.1485000000 |
| DA02959106 | 5.0000000000 |  44.4007000000 |
| DA02959106 | 4.0000000000 |  76.6485000000 |

I want to create a query that extracts the following information from the table:

id, AVG of 'val' for 'pass' = 1, AVG of 'val' for 'pass' = 2, etc

The result of the query should look like this:

+------------+---------+---------+---------+---------+---------+---------+---------+
| id         | val_1   | val_2   | val_3   | val_4   | val_5   | val_6   | val_7   |
+------------+---------+---------+---------+---------+---------+---------+---------+
| DA02959106 | 186.147 | 148.266 | 111.905 | 76.3985 | 44.4007 | 0       | 0       |
+------------+---------+---------+---------+---------+---------+---------+---------+

with more rows for each unique ‘id’, of course.

I already tried some queries like

SELECT id, pass, AVG(val) AS val_1 FROM data_r1 WHERE pass = 1 GROUP BY id;

This returns the correct result, but I have to expand it with results for the other possible values of ‘pass’ (up to 7)

I tried to use a nested SELECT within AVG but this didn’t work because I didn’t figure out how to correctly limit it to the current ‘id’.

I then created Views to represent the result of each query for ‘pass’ = 1, ‘pass’ = 2, etc. But for most ids the highest value for ‘pass’ is 5. When using JOIN queries to get the final result from the views I received an empty result set, because some of the Views are empty / don’t have values for a specific ‘id’.

Any ideas?

  • 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-04T06:13:09+00:00Added an answer on June 4, 2026 at 6:13 am

    If I understand what you need, try this:

    SELECT id, pass, AVG(val) AS val_1 
    FROM data_r1 
    GROUP BY id, pass;
    

    Or, if you want just one row for every id, this:

    SELECT d1.id,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 1) as val_1,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 2) as val_2,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 3) as val_3,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 4) as val_4,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 5) as val_5,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 6) as val_6,
        (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
         WHERE d2.id = d1.id AND pass = 7) as val_7
    from data_r1 d1
    GROUP BY d1.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problems sending an id though jquery. I have a form select
I have some problems with controlling a while loop inside an event structure. Say
I have come across an annoying problem while writing some PHP4 code. I renamed
I am writing some documentation and I have a little vocabulary problem: http://www.example.com/en/public/img/logo.gif is
I have some problems sending mails through SMTP using Spring's MailSender interface and the
I have some problems with OpenCV s cvCanny(...) and the Image data types it
i have some problems with rowspan: var doc1 = new Document(); doc1.SetPageSize(PageSize.A4.Rotate()); string path
I have some problems with mod_rewrited at .httacess. We have created a website, the
I have some problems to start programming using cocos2D on linux + android. I
I have some problems with a java app i'm developing, i'm using HtmlCleaner 2.2

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.