Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://rpbouman.blogspot.com/2005/10/creating-crosstabs-in-mysql.html" rel="nofollow">http://rpbouman.blogspot.com/2005/10/creating-crosstabs-in-mysql.html</a> for the clear instructions for getting here.</p> <p><strong>Lines 1-20</strong> - Set up a table to use for testing.</p> <p><strong>Lines 22-29</strong> - a 'static' crosstab query, assuming I know how many requirements I've got. Thanks D Mac for the solution you gave :)</p> <p><strong>Lines 30-44</strong> - A query that dynamically generates the static query above. </p> <p><strong>Lines 45-72</strong> - 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?</p> <pre><code>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; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload