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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:21:26+00:00 2026-06-17T03:21:26+00:00

in MySql database I have to make some little tricky query. I must get

  • 0

in MySql database I have to make some little tricky query.
I must get result only in TWO COLUMNS (TYPE (PROJECT,SUPPORT,EDUCATION) and VALUE (60 and 40 on example))

I have static table which contains types:

T1

ID  TYPE
1   PROJECT
2   PROJECT
3   PROJECT
4   SUPPORT
5   EDUCATION

I have static table T2 with all tasks that has value of some of the type:

    T2

  TASK    T1ID

    1      1
    2      1
    3      2
    4      3 
    5      4
    6      5

T3 is DYNAMIC table of created tasks with some IDS.

T3 

ID
1
2
3
4
5
6

T4 table has information who created those tasks.

T4

T3ID     Name
1       Bob Marley
2       Bob Marley
3       Bob Marley
4       Bob Marley
5       Tom Cruise
6       Bob Marley

and finally T5 contains info from T2 and T3

T5

TASK    T3ID
  1       1
  2       2
  3       3
  5       4
  1       5
  5       6

For some specific person (always one person- in this example Bob Marley or Tom Cruise)
I must haave statistics in PERCENTAGE how many they worked on PROJECTS or SUPPORT or EDUCATION.

So I will select Bob Marley- his T3ID is 1,2,3,4 and 6.

In table 5 his tasks ID are are 1,2,3,5 and again 5.

So when I look in table T2 his T1 IDs are 1,1,2,4 and 4.
It means that Bob Marley worked on 3 PROJECTS and 2 SUPPORT Tasks.

So when I select Bob Marley result should be:

PROJECT  60
SUPPORT  40

This is the final result from query.
How can I achieve this?
I know that it must be a couple of JOIN statements and of course you can use in WHERE clause

where name='Bob Marley'

If I put

 `where name=Tom Cruise

result should be

PROJECT 100`

Thank you

  • 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-17T03:21:27+00:00Added an answer on June 17, 2026 at 3:21 am

    I think this is what you are looking for:

    SELECT 
      t4.Name,
      SUM(CASE WHEN t1.type = 'PROJECT'   THEN 1 ELSE 0 END) * 100 / COUNT(t1.type) Project,
      SUM(CASE WHEN t1.type = 'SUPPORT'   THEN 1 ELSE 0 END) * 100 / COUNT(t1.type) SUPPORT,
      SUM(CASE WHEN t1.type = 'EDUCATION' THEN 1 ELSE 0 END) * 100 / COUNT(t1.type) Education
    FROM T1
    INNER JOIN T2 ON t1.ID = t2.T1ID
    INNER JOIN t5 ON t2.task = t5.task
    INNER JOIN t3 ON t3.id = t5.t3id
    INNER JOIN t4 ON t3.Id = t4.t3id
    GROUP BY t4.Name;
    

    SQL Fiddle Demo

    This will give you:

    |       NAME | PROJECT | SUPPORT | EDUCATION |
    ----------------------------------------------
    | Bob Marley |      60 |      40 |         0 |
    | Tom Cruise |     100 |       0 |         0 |
    

    for the sample data you posted.

    Note that: This will give you only the Name that was involved in projects and tasks. You might need to OUTER JOIN(LEFT or RIGHT) the table instead of INNER JOIN to include those Names that were not involved in any tasks or projects (the unmatched rows), with IFNULL() to replaces NULLs with zeros.


    Update:

    You can use the CONCAT() function to do this, but it will be easier and more readable if you include the previous query inside a subquery and do this in an outer query like so:

    SELECT
      Name,
      CONCAT(CAST(project   AS DECIMAL(10, 2)), ' AND ', 
             CAST(support   AS DECIMAL(10, 2)), ' AND ', 
             CAST(education AS DECIMAL(10, 2))) AS Percentage
    FROM
    (
      SELECT 
        t4.Name,
        SUM(CASE WHEN t1.type = 'PROJECT' THEN 1 ELSE 0 END) * 100 / COUNT(t1.type) Project,
        SUM(CASE WHEN t1.type = 'SUPPORT' THEN 1 ELSE 0 END) * 100 / COUNT(t1.type) SUPPORT,
        SUM(CASE WHEN t1.type = 'EDUCATION' THEN 1 ELSE 0 END) * 100 / COUNT(t1.type) Education
      FROM T1
      INNER JOIN T2 ON t1.ID = t2.T1ID
      INNER JOIN t5 ON t2.task = t5.task
      INNER JOIN t3 ON t3.id = t5.t3id
      INNER JOIN t4 ON t3.Id = t4.t3id
      GROUP BY t4.Name
    ) t;
    

    Updated SQL Fiddle Demo

    This will give you:

    |       NAME |               PERCENTAGE |
    -----------------------------------------
    | Bob Marley | 60.00 AND 40.00 AND 0.00 |
    | Tom Cruise | 100.00 AND 0.00 AND 0.00 |
    

    You can add the WHERE clause to limit this values to only any user name like WHERE Name = 'Some name' in the outer query or in the subquery.


    Update 2

    OK, Sorry I missed that. There is no need to PIVOT or CONCAT() or dynamic SQL to do so. You can do this like so:

    SELECT 
      t1.type,
      COUNT(t2.task) * 100 / 
      (SELECT COUNT(*)
       FROM T4 t 
       WHERE t4.Name = t.Name) AS Percentage
    FROM T1
    INNER JOIN T2 ON t1.ID = t2.T1ID
    INNER JOIN t5 ON t2.task = t5.task
    INNER JOIN t3 ON t3.id = t5.t3id
    INNER JOIN t4 ON t3.Id = t4.t3id
    WHERE t4.Name = 'Bob Marley' 
    GROUP BY t4.Name, t1.type;
    

    Updated SQL Fiddle Demo

    This will give you:

    |    TYPE | PERCENTAGE |
    ------------------------
    | PROJECT |         60 |
    | SUPPORT |         40 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue. I am getting data from a MySQL database, and make
In mysql database i have this column called: Name: Date Type: datetime I have
In a MySQL database I have two tables linked in a join. One table
I have a Linux web server and I'd like to make some database tables
I'm in the process of cleaning up a MySQL database and have to make
I have little php code for daily audit transaction.I make simple to get the
I have numbers saved as VARCHAR to a MySQL database. I can not make
I have a database (MySQL) that holds cost records for some of our customers
I have to make a report from very old mysql database (about year 2003).
I have some Categories (numbers) in a MYSQL database that I am trying to

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.