Surely order by data1+data2 gives the same top 5 names just sorted differently whereas if you do separate queries the top 5 names might be different. To do both in 1 query I guess you could do something complicated like this

#!perl use strict; use DBI; my $DBH; my $data1; my $data2; datas('dbname'); print "$data1\n$data2"; sub datas { $data1 = ""; $data2 = ""; my $str = shift; $DBH = &connect or die "Cannot connect to the sql server \n"; $DBH->do("USE $str;"); my $stmt=" ( select name,data1 from datas order by data1 desc limit 5 ) union all ( select 'added' ,sum(data1) from ( select data1 from datas order by data1 desc limit 18446744073709551615 offset 5 ) t1 ) union all ( select name,data2 from datas order by data2 desc limit 5 ) union all ( select 'added' ,sum(data2) from ( select data2 from datas order by data2 desc limit 18446744073709551615 offset 5 ) t1 )"; my $sth = $DBH->prepare( $stmt ); $sth->execute() or die $sth->errstr; my $tmp = 0; while(my @row_array=$sth->fetchrow_array) { if ($tmp > 5){ if ($tmp == 6 ) { $data2 = "\[\"$row_array[0] \($row_array[1]\)\",$row_array[1]\ +]"; } else { $data2 .= ",\[\"$row_array[0] \($row_array[1]\)\",$row_array[1 +]\]"; } } else { if ($tmp == 0 ) { $data1 = "\[\"$row_array[0] \($row_array[1]\)\",$row_array[1] +\]"; } else { $data1 .= ",\[\"$row_array[0] \($row_array[1]\)\",$row_array[ +1]\]"; } } ++$tmp; } $sth->finish; $DBH->disconnect(); }
poj

In reply to Re^5: perl sql to separate and sort the column separately? by poj
in thread perl sql to separate and sort the column separately? by gpssana

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.