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,
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;
| [reply] [d/l] |
|
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 | [reply] [d/l] |
|
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;
| [reply] [d/l] |
|
|
|
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();
}
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
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
)
| [reply] [d/l] [select] |
|
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
|
|