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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:03:41+00:00 2026-05-31T07:03:41+00:00

Suppose I have this database table (some sample code below) that stores the relationship

  • 0

Suppose I have this database table (some sample code below) that stores the relationship between two lists (requirements and testcases in my case) and I want to create a table with rows showing testcases and columns showing requirements with an indicator showing that a relationship exists.

A few limitations

  • I don’t have the luxury of changing the db structure as this belongs to an open source test case management system (TestLink).
  • It’s possible to write some code for this, but I’m hoping it can be done in MySQL.
  • Ah, and yes, it uses MySQL, so this would have to work in that environment.
  • This functionality used to exist, but has been taken out because typically, this type of work brings the db to its knees when there are tens-of-thousands of testcases and requirements.

    create table pivot (
    req_id int(11),
    testcase_id int(11)
    ) ;

    /*Data for the table pivot */

    insert into pivot(req_id,testcase_id) values (1,1);

    insert into pivot(req_id,testcase_id) values (2,2);

    insert into pivot(req_id,testcase_id) values (3,3);

    insert into pivot(req_id,testcase_id) values (4,1);

    insert into pivot(req_id,testcase_id) values (5,2);

    insert into pivot(req_id,testcase_id) values (6,3);

    insert into pivot(req_id,testcase_id) values (2,1);

    insert into pivot(req_id,testcase_id) values (3,2);

What I want to get out of the query is a table that looks somethign like this:

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

Note:the row are the testcase_ids and the columns are the ‘req_ids’

Anyone have a tip on how to get this with just SQL?

  • 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-31T07:03:43+00:00Added an answer on May 31, 2026 at 7:03 am

    I now have a name for what I’m trying to accomplish. It’s a ‘dynamic crosstab’. Here is how I got to the solution. Thanks to http://rpbouman.blogspot.com/2005/10/creating-crosstabs-in-mysql.html for the clear instructions for getting here.

    Lines 1-20 – Set up a table to use for testing.

    Lines 22-29 – a ‘static’ crosstab query, assuming I know how many requirements I’ve got.
    Thanks D Mac for the solution you gave 🙂

    Lines 30-44 – A query that dynamically generates the static query above.

    Lines 45-72 – This is where I’m having the problem. The intent is to create a stored procedure that returns the result of the dynamic query. MySQL is saying there is a syntax issue, but I don’t see how to fix it. Any thoughts?

    drop table if exists pivot;
    
    create table `pivot` ( 
    `req_id` int(11), 
    `testcase_id` int(11) 
    ); 
    
    /*Data for the table `pivot` */ 
    
    insert into `pivot`(`req_id`,`testcase_id`) values (1,4); 
    insert into `pivot`(`req_id`,`testcase_id`) values (2,4); 
    insert into `pivot`(`req_id`,`testcase_id`) values (3,4); 
    insert into `pivot`(`req_id`,`testcase_id`) values (4,7); 
    insert into `pivot`(`req_id`,`testcase_id`) values (1,7); 
    insert into `pivot`(`req_id`,`testcase_id`) values (2,12); 
    insert into `pivot`(`req_id`,`testcase_id`) values (3,12); 
    insert into `pivot`(`req_id`,`testcase_id`) values (4,4); 
    
    select * from pivot;
    
    select testcase_id
    ,        if(sum(req_id = 1), 1, 0)
    ,        if(sum(req_id = 2), 1, 0)
    ,        if(sum(req_id = 3), 1, 0)
    ,        if(sum(req_id = 4), 1, 0)
    from pivot
    group by testcase_id;
    
    select concat(
          'select testcase_id','\n'
          , group_concat(
             concat(
                    ',        if(sum(req_id = ',p2.req_id,'), 1, 0)','\n'
              )
              order by p2.req_id
              separator ''
            )
          , 'from pivot','\n'
          , 'group by testcase_id;','\n'
          ) statement
    from pivot p2
    order by p2.req_id;
    
    CREATE PROCEDURE p_coverage()
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    begin
        select concat(
                      'select testcase_id','\n'
                      , group_concat(
                         concat(
                                ',        if(sum(req_id = ',p2.req_id,'), 1, 0)','\n'
                          )
                          order by p2.req_id
                          separator ''
                        )
                      , 'from pivot','\n'
                      , 'group by testcase_id;','\n'
                      ) statement
        into @coverage_query
        from pivot p2
        order by p2.req_id;
    
        prepare coverage from @coverage_query;
    
        execute coverage;
    
        deallocate prepare coverage;
    end;
    
    select * from pivot;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose, I have saved some permissions in the database by using this code: RoleRepository
Suppose that I have a database table that contains information on cities across the
I have some table and records in a database, that I use to manually
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have 2 tables in a database. eg: Dog & Boss This is
Suppose I have a database like this: This is set up to give role-wise
I have like 400 records on my database, suppose this is the first version
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
I have an application that is saving clob data into database. Suppose when I

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.