Hi, all, I could not find the solution for my problem.

I try to pivot several mySQL tables for reporting, the row number is not fixed so what I am doing now is that grouping it first as a subquery and then pivot it. It works fine with small number of rows but does not work with large number rows.

Here is an example, their are many cols, but only prod matters to me.

prod xx xx ... 'prod 1' 'prod 2' 'prod 1' 'prod 3' 'Prod 4' ......
After grouping by prod, the table should be
prod count prod 1 10 prod 2 15 ......

Here is my code:

use DBI; my $dbh = connect(....); my $pvt_col = 'prod'; my $count = 'count'; my $table = 'prod' my $stmt = q{ SELECT CONCAT( '(SUM(CASE WHEN }. $pvt_col. q{ IS NULL THEN Count ELSE 0 END)) AS + Total, ', GROUP_CONCAT(DISTINCT CONCAT( '(SUM(CASE WHEN }. $pvt_col. q{ = ''', }. $pvt_col. q{, ''' T +HEN ', }. $count. q{, ' ELSE 0 END)) AS `', }. $pvt_col. q{, '`' ) ) ) FROM ( SELECT }. $pvt_col. q{, count(*) AS }. $count. q{ FROM }. $table. q{ GROUP BY }. $pvt_col. q{ WITH ROLLUP) as temptbl;}; my $sth = $dbh->prepare($stmt); $sth->execute(); my $tmp; while ( ($tmp) = $sth->fetchrow_array() ) { print"$tmp\n"; last; }

The expected output should be:

(SUM(CASE WHEN prod IS NULL THEN Count ELSE 0 END)) AS Total, (SUM(CASE WHEN prod = 'prod1' THEN 10 ELSE 0 END)) AS `prod1`, (SUM(CASE WHEN prod = 'prod2' THEN 15 ELSE 0 END)) AS `prod2`, (SUM(CASE WHEN prod = 'prod3' THEN 1 ELSE 0 END)) AS `prod3`, .....

After this, I can query again to produce pivot results:

my $statement = q{ SELECT }. $tmp. q{ FROM( SELECT }. $pvt_col. q{, count(*) AS }. $count. q{ FROM }. $table. q{ GROUP BY }. $pvt_col. q{ WITH ROLLUP ) as temptbl;}; $sth = $dbh->prepare($statement); ....
$statement is like:
SELECT ( (SUM(CASE WHEN prod IS NULL THEN Count ELSE 0 END)) AS Total, (SUM(CASE WHEN prod = 'prod1' THEN 10 ELSE 0 END)) AS `prod1`, (SUM(CASE WHEN prod = 'prod2' THEN 15 ELSE 0 END)) AS `prod2`, (SUM(CASE WHEN prod = ..... ... FROM( SELECT prod, count(*) AS Count FROM prod GROUP BY prod WITH ROLL +UP) as temptbl;

It works beautifully for tables with small number of rows, but failed for tables with larger number of rows. $tmp is trucated when I have large rows. So $statement cannot be excuted successfully.

Monks, any help is highly appreciated!

Environment: Perl 5.14.2, ubuntu 12.04.2, MySQL 5.5.31


In reply to return string from query (DBI, MySQL) got truncated when trying to generating statement dynamically for MySQL by khandielas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.