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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:58:48+00:00 2026-05-31T13:58:48+00:00

I want to create group query where Table values are like this below: EMP_ID

  • 0

I want to create group query where Table values are like this below:

EMP_ID  ProjectID
815     1
985     1
815     3
985     4
815     4

And i want output like this

EMP_ID ProjectID1 ProjectID2 ProjectID3
815    1          3          4
985    1          4          0

can anyone know how can i achieve this thing in SQL query.

Thank in advance.

  • 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-05-31T13:58:49+00:00Added an answer on May 31, 2026 at 1:58 pm

    The short way:

    Using http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

    SELECT
      tbl.emp_id,
      GROUP_CONCAT( DISTINCT project_id ) project_id_list
    FROM tbl
    GROUP BY tbl.emp_id
    

    In this case, you have to split/process the concatenated project_id_list string (or NULL) in your application

    The long way:

    We will use a little trick:

    http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html

    For MyISAM tables you can specify AUTO_INCREMENT on a secondary column
    in a multiple-column index. In this case, the generated value for the
    AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1
    WHERE prefix=given-prefix. This is useful when you want to put data
    into ordered groups.

    CREATE TEMPORARY TABLE temp (
      emp_id      INT NOT NULL,
      -- project_num will count from 1 to N PER emp_id!
      project_num INT NOT NULL AUTO_INCREMENT,
      project_id  INT NOT NULL,
      PRIMARY KEY ( emp_id, project_num )
    ) ENGINE=MyISAM; -- works only with myisam!
    

    Generate the per-group auto increments:

    INSERT INTO temp ( emp_id, project_id )
    SELECT emp_id, project_id FROM tbl
    

    Calculate how many project_id columns are needed:

    $MAX_PROJECTS_PER_EMP =
      SELECT MAX( max_projects_per_emp ) FROM
        ( SELECT COUNT(*) AS max_projects_per_emp project_id FROM tbl GROUP BY emp_id )
    

    Programmatically create the select expression:

    SELECT
      temp.emp_id,
      t1.project_id  AS project_id_1,
      t2.project_id  AS project_id_2,
      t98.project_id AS project_id_98,
      t99.project_id AS project_id_99,
    FROM      temp
    LEFT JOIN temp AS t1 ON temp.emp_id = t1.id AND t1.project_num = 1
    LEFT JOIN temp AS t2 ON temp.emp_id = t2.id AND t1.project_num = 2
    // create $MAX_PROJECTS_PER_EMP lines of LEFT JOINs
    LEFT JOIN temp AS t98 ON temp.emp_id = t98.id AND t98.project_num = 98
    LEFT JOIN temp AS t99 ON temp.emp_id = t99.id AND t99.project_num = 99
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
I am trying to create a conversations based messaging system. I want to group
I want to run a linq query that will return values to my custom
This is a script to produce the dataset of a nested table: create table
The situation is, that i need to create table in grid view looking like
I have mysql table with data like this. record will have server with total
Let's say I have a payments table like so: CREATE TABLE Payments ( PaymentID
I am trying to write a query to create a 'table' of data as
I want to create a stored procedure which takes integer values as a @top
Using the MySQL query below, I have created a pivot table which is pretty

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.