【问题标题】:postgres typeorm return untill sum of a column is greater than conditionpostgres typeorm 返回直到列的总和大于条件
【发布时间】:2021-04-27 02:00:15
【问题描述】:

我有一个实体,它的列类型为 bigint,名为 price。

这是一张非常大的桌子。我想从这个表中获取数据,直到价格大于或等于一个数字。

如何在不从结果中获取整个表格的情况下实现这一目标

@Entity()
export class myTable extends BaseEntity {
  @Column({type:"bigint})
  price:number
}

我想获取实体,直到价格总和大于或等于一个数字

const input: number = 50000
const result = await myTable.find(/*order by date and where sum (price) >= input*/)

语法是什么,我该怎么做?使用 baseEntity 查询或 queryBuilder,最好不要使用原始查询结果

【问题讨论】:

    标签: postgresql aggregation typeorm


    【解决方案1】:

    我认为您需要为此使用query builder 创建一个原始查询。查询可参考this

    【讨论】:

    • 我有下面的查询:`SELECT * from (SELECT *, sum(price) over (order by datel asc) as total from orders) t where total
    • 我无法理解你想表达的意思
    • 假设我有 2000$,orderdBy date 的订单价格是:5k、3k、10k、4k、12k 等。我只想退回到 4k,因为之后比用户还多可以买。我该怎么做? SELECT * from ( SELECT *, sum(price) over (order by datel asc) as total from orders ) t where total <= 20000 不返回我想要的。因为任何低于第一个价格的东西它都返回空。您认为最好在此处编写什么查询来优化我的查询?
    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2021-07-21
    • 2022-10-24
    相关资源
    最近更新 更多