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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:55:15+00:00 2026-06-16T20:55:15+00:00

I have a table with data in a following format region gender age_group population

  • 0

I have a table with data in a following format

region   gender  age_group  population 

Dallas    M        1          200  
Dallas    F        1          500 
NY        M        1          320 
NY        F        1          310 
Dallas    M        2          300 
Dallas    F        2          600 
NY        M        2          400 
NY        F        2          800 

I’m looking for the best query to output my data in the following format

region/age_group      1           2
Gender             M     F       M    F
Dallas             200   500    300  600
NY                 320   310    400  800

my query returns only the top colum and region. Not sure how to sub divide the top colum into genders as appearing above

SELECT distinct (region), a.group_one, b.group_two 
FROM city_populations c  
LEFT JOIN city_populations AS a ON a.age_group=1 AND c.age_group=a.age_group  
LEFT JOIN city_populations AS b ON b.age_group=2 AND c.age_group=a.age_group  
  • 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-16T20:55:17+00:00Added an answer on June 16, 2026 at 8:55 pm

    //First i fetched all data from database

    $result = mysql_query("select * from test2")or die(mysql_error());
    $regions = array();
    $age_groups = array();
    while($det = mysql_fetch_array($result)){
    
        if(!in_array($det['region'], $regions)){
            $regions[] = $det['region'];
        }
        if(!in_array($det['age_group'], $age_groups)){
            $age_groups[] = $det['age_group'];
        }
        $rows[] = $det;
    }
    

    //then processed in php to show as you wanted

    echo "<table>";
    echo "<tr><td>Age group</td>";
    foreach($age_groups as $keyAG => $valueAG){
        echo "<td colspan='2'>$valueAG</td>";
    }
    echo "</tr>";
    echo "<tr><td>Gender</td>";
    foreach($age_groups as $keyAG => $valueAG){
        echo "<td>M</td>";
        echo "<td>F</td>";
    }
    echo "</tr>";
    foreach($regions as $keyR => $valueR){
    
        echo "<tr>";
        echo "<td>".$valueR."</td>";
        foreach($age_groups as $keyAG => $valueAG){
    
            foreach($rows as $keyROWS => $valueROWS){
                if($valueROWS['region'] == $valueR && $valueROWS['age_group'] == $valueAG){
                    if($valueROWS['gender'] == 'M'){
                        echo "<td>".$valueROWS['population']."</td>";
                    }
                    elseif($valueROWS['gender'] == 'F'){
                        echo "<td>".$valueROWS['population']."</td>";
                    }
                }
            }
            $data[$valueR][$valueAG] = array($maleValue, $femaleValue);
    
        }
        echo "</tr>";
    }
    echo "</table>";
    

    Note: It is highly recommended to use mysqli_* instead of mysql.

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

Sidebar

Related Questions

I have data in a table in following format ID DocNumber RevOrder 1 DOC-001
I have data in the following format in a sql server database table [CPOID]
I have a table that has the following format for the data. All I
I have a log table with the following format (and sample data): date |
If I have a table with data in the following format: id itemID projectID
I have data in the following format in a table: Acct# Amount 123 3.4
I have a table in csv format, the data is the following: 1 3
Suppose we have the following table data: ID parent stage submitted 1 1 1
I have table that I insert data with following query (from c# code): INSERT
Lets say i have a table with the following data Customer table: Name amount

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.