简体   繁体   English

带有理论的累积DQL

[英]Cumulative DQL with Doctrine

Im having a hard time working out a proper DQL to generate cumulative sum. 我很难找出合适的DQL以生成累积和。 I can do it in plain SQL but when it comes to DQL i cant get hold of it. 我可以用普通的SQL来完成它,但是当涉及到DQL时,我无法掌握它。

Here is how it looks in SQL: 这是在SQL中的外观:

  SELECT s.name, p.date_short, p.nettobuy, (select sum(pp.nettobuy) as sum from price pp where pp.stock_id = p.stock_id and p.broker_id = pp.broker_id and pp.date_short <= p.date_short) as cumulative_sum FROM price p left join stock s on p.stock_id = s.id group by p.stock_id, p.date_short order by p.stock_id, p.date_short 

Thanks 谢谢

Hey, I have check the documentation for Doctrine 1.2, and the way to create the query is (put attention on the alias): 嘿,我检查了Doctrine 1.2的文档,创建查询的方法是(请注意别名):

$query = Doctrine_Query::create();
$query->addSelect('AVG(price) as price');
$query->addSelect('AVG(cost) as cost');
// as many addSelect() as you need
$query->from('my_table');

To output the SQL query created: 要输出创建的SQL查询:

echo $query->getSqlQuery();

To execute the statement: 要执行该语句:

$product = $query->fetchOne();

And to access the retrieved data is: 访问检索到的数据是:

echo $product->getPrice();
echo $product->getCost();

Read the rest of the documentation at Group By Clauses . 阅读Group By Clauses中的其余文档。

You just specify the sum in your select part of the DQL: 您只需在DQL的选定部分中指定总和:

$query = Doctrine_Query::create()
   ->select('sum(amount)')
   ->from('some_table');

Check out this page in the Doctrine documentation for more info. 请查阅《 Doctrine》文档中的此页面以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM