Doctrine 的累积 DQL

2022-01-09 00:00:00 sum php doctrine dql

我很难制定出合适的 DQL 来生成累积总和.我可以用普通的 SQL 来做,但是当涉及到 DQL 时,我无法掌握它.

Im having a hard time working out a proper DQL to generate cumulative sum. I can do it in plain SQL but when it comes to DQL i cant get hold of it.

这是它在 SQL 中的样子:

Here is how it looks in 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

谢谢

推荐答案

你好,我查看了 Doctrine 1.2 的文档,创建查询的方法是(注意别名):

Hey, I have check the documentation for Doctrine 1.2, and the way to create the query is (put attention on the alias):

$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');

输出创建的 SQL 查询:

To output the SQL query created:

echo $query->getSqlQuery();

执行语句:

$product = $query->fetchOne();

访问检索到的数据是:

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

阅读 按子句分组.

相关文章