如何计算返回查询的MySQL临时表的中位数

时间:2017-04-24 06:49:00

标签: java mysql

我已经从一个大型数据库创建了一个临时表,我必须为此计算medain。

下面用于创建临时表的代码(它有21行vol with numbers)

$(".category_name").hover(function(){
  //alert("coming");
  $('#product_183').show();
  $('#product_184').hide();
  // alert(product_id);
});

下面的代码计算中位数,但没有得到如何为上面创建的临时表“table1”

CREATE TEMPORARY TABLE table1 (Select vol from EOD_PRICING where 
`ticker`=16665396 order by `xdate` desc limit 21);    

如果我将下面的代码与UNION ALL一起用于我的要求,它说  SELECT AVG(middle_values) AS 'median' FROM ( SELECT t1.`vol` AS 'middle_values' FROM ( SELECT @row:=@row+1 as `row`, x.`vol` FROM table1 AS x, (SELECT @row:=0) AS r WHERE `ticker`=16665396 order by `xdate` desc limit 21 ORDER BY x.`vol` ) AS t1, ( SELECT COUNT(*) as 'count' FROM table1 x WHERE `ticker`=16665396 order by `xdate` desc limit 21 ) AS t2 WHERE t1.row >= t2.count/2 and t1.row <= ((t2.count/2) +1)) AS t3;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 17

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

解决了......对于偶数和奇数都得到了正确答案。

<?php require_once 'dompdf/autoload.inc.php';
require_once'dompdf/autoload.inc.php' $dompdf = new Dompdf\Dompdf();
$html =  $this->load->view ('visitor_data',[],true);
$dompdf-> load_html ($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();
// Get the generated PDF file contents

$pdf = $dompdf->output();

// Output the generated PDF to Browser
$dompdf->stream("visitor-data",array("Attachment"=>0));    ?>

感谢您的回复。