使用knex的物化视图迁移错误

时间:2018-10-31 10:50:00

标签: sql postgresql knex.js

我有一个由公司指导员编写的SQL查询,该查询创建了一个包含voucher_Id,procedure TForm1.FormCreate(Sender: TObject); begin AddClearButton(cxDBLookupComboBox1.Properties); end; procedure TForm1.AddClearButton(Prop: TcxCustomEditProperties); begin with Prop.Buttons.Add do begin Kind:= bkText; Caption:= #$2715; //Unicode X-symbol end; Prop.OnButtonClick:= ClearButtonClick; end; procedure TForm1.ClearButtonClick(Sender: TObject; AButtonIndex: Integer); begin if AButtonIndex = 1 then with TcxCustomEdit(Sender) do begin EditValue:= Null; PostEditValue; end; end; quantityused列的表,但是,他向我展示了一个示例,需要在我的postgres数据库中并将其写在查询工具内部。我做了一些小的修改,但是当我尝试通过迁移创建实体化视图时,以下查询抛出错误:

这是他在postgres的查询工具中写的查询,它的工作原理像一个超级按钮:

remaining

然后在使用knex进行迁移时,我有:

SELECT v.voucher_id,v.quantity, COUNT(ov.voucher_id) AS "used", v.quantity - COUNT(ov.voucher_id) AS "remaining"
      FROM public.vouchers v
      LEFT OUTER JOIN public.order_vouchers ov ON (v.voucher_id = ov.voucher_id)
      GROUP BY v.voucher_id, v.quantity

该错误专门抛出在CREATE MATERIALIZED VIEW mv_vouchers SELECT * FROM vouchers, SELECT v.voucher_id,v.quantity, COUNT(ov.voucher_id) AS "used", v.quantity - COUNT(ov.voucher_id) AS "left" FROM public.vouchers v LEFT OUTER JOIN public.order_vouchers ov ON (v.voucher_id = ov.voucher_id) GROUP BY v.voucher_id, v.quantity 附近,我正在使用的表是SELECTvouchers。对此我很陌生,非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您有两个SELECT子句。这就是你想要的吗?

CREATE MATERIALIZED VIEW mv_vouchers as
      SELECT v.voucher_id, v.quantity, COUNT(ov.voucher_id) AS "used", v.quantity - COUNT(ov.voucher_id) AS "left"
      FROM public.vouchers v LEFT OUTER JOIN
           public.order_vouchers ov
           ON v.voucher_id = ov.voucher_id
      GROUP BY v.voucher_id, v.quantity;