Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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 ( #1186821=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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?

> 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,

Replies are listed 'Best First'.
Re^4: How to add columns with new row name using perl from mysql query?
by chacham (Prior) on Apr 03, 2017 at 19:09 UTC

    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

        Tested on MySQL

        And added a missing comma, FROM, and table alias. (Oops!) Thank you. Why did you remove the case statement from the GROUP BY? That's the part that does the magical split. :) (Even if i did write it incorrectly.) If mysql won't support it there, it'd need to be added into the subquery, which begins to make it more confusing...

        SELECT CASE WHEN @rownum <= @row_limit THEN queue_name ELSE 'others' END +queue_name, SUM(jobs_pend) jobs_pend, SUM(jobs_run) jobs_run FROM ( SELECT CASE WHEN @rownum <= @row_limit THEN @rownum ELSE @row_limit + + 1 END box, queue_name, jobs_pend, jobs_run FROM ( 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 ) q ) p GROUP BY box;
Re^4: How to add columns with new row name using perl from mysql query?
by finddata (Sexton) on Apr 06, 2017 at 12:08 UTC
    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.
Re^4: How to add columns with new row name using perl from mysql query?
by finddata (Sexton) on Apr 07, 2017 at 05:52 UTC
    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://1186821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-10-27 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (256 votes). Check out past polls.

    Notices?