Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: How to add columns with new row name using perl from mysql query?

by huck (Parson)
on Apr 03, 2017 at 11:21 UTC ( #1186813=note: print w/replies, xml ) Need Help??


in reply to Re: How to add columns with new row name using perl from mysql query?
in thread How to add columns with new row name using perl from mysql query?

Note the error message for your select is wrong here

Hey at least its not "select *' anymore!

Re^3: How to calculate the column and print it in pie chart format using perl?

Reminds me of a pre-punk band i did some gigs with, went by "Mod Dirge", but then i started seeing them doing gigs as Yer Mom" as well, at the same time even. The answer as to "why?" was that when the clubs wouldnt book Mod Dirge anymore cuz of the rowdy crowd they brought with them, they would send another guy to the club to get them booked under "Yer Mom", and then they invited an even "rowdier" crowd. They got another dozen or two gigs that way too.

  • Comment on Re^2: How to add columns with new row name using perl from mysql query?

Replies are listed 'Best First'.
Re^3: How to add columns with new row name using perl from mysql query?
by choroba (Archbishop) on Apr 03, 2017 at 13:35 UTC
    > Hey at least its not "select *' anymore!

    Note that my solution involves SELECT * from a union of two tables both of which have the columns listed, using the same names for both the inner selects would obscure the already complex statement even more:

    SELECT queue_name, jobs_pend, jobs_run FROM ( SELECT queue_name, jobs_pend, jobs_run FROM queues ORDER BY queue_name LIMIT 5 ) UNION ALL SELECT 'others', SUM(jobs_pend) AS jobs_pend, SUM(jobs_run) AS jobs_ru +n FROM ( SELECT jobs_pend, jobs_run FROM queues ORDER BY queue_name LIMIT -1 OFFSET 5 )

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Good solution, and excellent use of UNION ALL.

      Based on you solution, i would like to provide another approach, that uses one query, and without LIMIT or OFFSET. It simply adds a row number. Though i do not know how mysql treats an ORDER BY clause in a subquery. Anyway, completely untested:

      SELECT CASE WHEN @rownum <= @row_limit THEN queue_name ELSE 'others' END +queue_name, SUM(jobs_pend) jobs_pend, SUM(jobs_run) jobs_run ( SELECT @row_num := @row_num+1 queue_name, jobs_pend, jobs_run FROM queues, (SELECT @row_num := 0, @row_limit := 5) r ORDER BY queue_name ) GROUP BY CASE WHEN @rownum <= @row_limit THEN @rownum ELSE @rownum + 1 END;

        Tested on MySQL

        SELECT CASE WHEN num <= @row_limit THEN queue_name ELSE 'others' END queue_name, sum(jobs_pend) jobs_pend, sum(jobs_run) jobs_run FROM ( SELECT @row_num := @row_num+1 as num, queue_name, jobs_pend, jobs_run FROM queues, (SELECT @row_num := 0, @row_limit := 5) r ORDER BY queue_name ) q GROUP BY 1
        poj
      I HAD USED AS FOLLOWS BUT IT THROWS AN ERROR AS could not in7sert_run_pend data
      sub pending_running_partition { $var_data_running = ""; $var_data_pending = ""; my $str= shift; $DBH = &connect or die "Cannot connect to the sql server \n"; $DBH->do("USE $str;"); my $stmt="select queue_name,jobs_pending,jobs_running from(select +queue_name,jobs_pending,jobs_running from queues order by queue_name +limit 5)union all select 'others',sum(jobs_pending) as jobs_pending,s +um(jobs_running) as jobs_running from (select jobs_pending,jobs_runni +ng from queues order by queue_name limit -1 offset 5"; my $sth = $DBH->prepare( $stmt ); $sth->execute() or print "Could not in7sert_run_pend data"; my $tmp = 0; while(my @row_array=$sth->fetchrow_array) { if ($tmp == 0) { $var_data_running .= "\[\"$row_array[0] \($row_array[2]\)\",$r +ow_array[2]\]"; $var_data_pending .= "\[\"$row_array[0] \($row_array[1]\)\",$r +ow_array[1]\]"; $tmp++; } else { $var_data_running .= ",\[\"$row_array[0] \($row_array[2]\)\",$ +row_array[2]\]"; $var_data_pending .= ",\[\"$row_array[0] \($row_array[1]\)\",$ +row_array[1]\]"; } } $sth->finish; $DBH->disconnect(); }

        There is no error message COULD NOT INSERT DATA in your code.

        Please show the exact error message you get, and the exact code you are running.

        Also consider creating your database connection with the RaiseError flag so that DBI automatically tells you when an error happens.

        A reply falls below the community's threshold of quality. You may see it by logging in.
      Hai,
      Here the row others prints as NULL which as follows.How can i add data type for the others row in the below sql query.
      | others | NULL | NULL |
      SELECT queue_name, jobs_pend, jobs_run FROM ( SELECT queue_name, jobs_pend, jobs_run FROM queues ORDER BY queue_name LIMIT 5 ) UNION ALL SELECT 'others', SUM(jobs_pend) AS jobs_pend, SUM(jobs_run) AS jobs_ru +n FROM ( SELECT jobs_pend, jobs_run FROM queues ORDER BY queue_name LIMIT 5 OFFSET 5 )

        At Google no one can hear you grovel. (apologies to Alien)

        Proper indentation makes it much easier to make sure everything is balanced doesnt it?

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1186813]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2020-10-25 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?