Create two SQL queries and have the sorting there, according to the order you need. Then run each query separately.

I disagree, if only on a technicality. Back to the idea that these are statistics from a batch scheduler, the chance that the underlying data may have changed between the separate query runs is not zero. Instead i would run one select so there is just a single select query cursor instance, and sort the data separately in perl as in

use strict; use warnings; use DBI; use JSON 'encode_json'; use cheap::mysqls qw/sql_connect/; my ($DBH,$rc) = sql_connect(id=>'finddata',dbi=>'mysql'); $DBH->do('DROP view IF EXISTS datas '); $DBH->do('create view datas as select queue_name as name, jobs_pend as data1, jobs_run as data2 from queues '); { my @col1; my @col2; my @oth1; my @oth2; my $stmt="select name,data1,data2 from((select name,data1,data2 fr +om datas order by data1 +data2 desc limit 5 )union all(select 'added +' as name ,sum(data1) as data1 ,sum(data2) as data2 from (select dat +a1,data2 from datas order by data2+data1 desc limit 1844674407370955 +1615 offset 5 ) new)) new;"; my $sth = $DBH->prepare( $stmt ); $sth->execute() or die $sth->errstr; my $tmp = 0; while(my @row_array=$sth->fetchrow_array) { if ($row_array[0] ne 'added') { push @col1,[$row_array[0],$row_array[1]]; push @col2,[$row_array[0],$row_array[2]]; } else { push @oth1,[$row_array[0],$row_array[1]]; push @oth2,[$row_array[0],$row_array[2]]; } } $sth->finish; my @sort1=((sort {$b->[1] <=> $a->[1]} @col1),@oth1); my $json1 = encode_json( \@sort1 ); print $json1."\n"; my @sort2=((sort {$b->[1] <=> $a->[1]} @col2),@oth2); my $json2 = encode_json( \@sort2 ); print $json2."\n"; } $DBH->do('DROP view IF EXISTS datas '); $DBH->disconnect();

Since they included the json producing code for the creating of the pie charts, i have done so as well. That also explains why they want the separate sorts.

To also add to the incomplete specs in the OP, notice how what was once called the others row has become the added row, yet that is not exhibited in the expected output.

cheap::mysqls is a module in my private library that allows me to easily connect to my mysql and sqlite databases. you may use whatever method you like to connect to yours instead.


In reply to Re^4: perl sql to separate and sort the column separately? by huck
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.