【问题标题】:how to join more then two table in postgresql?如何在postgresql中加入两个以上的表?
【发布时间】:2015-07-16 09:17:13
【问题描述】:

我正在运行以下查询

select 
    to_char(sale_order.date_order ,'DD-MM-YYYY') , sum(sale_order.amount_total) as amount

from 
    public.sale_order
where
   sale_order.order_year = '2015' and
   sale_order.order_month = 'April'
group by 
   to_char(sale_order.date_order ,'DD-MM-YYYY') order by to_char(sale_order.date_order ,'DD-MM-YYYY') asc

它给出正确的输出

   to_char..     amount

"14-04-2015";   1298.00
"15-04-2015";   4294.00
"16-04-2015";   1398.00
"17-04-2015";   1927.00
"18-04-2015";   3094.00
"19-04-2015";   6988.00
"20-04-2015";   6641.00
"21-04-2015";   3045.00

但我正在尝试输入具有多个表连接的条件,然后它会给出不同的金额值

select 
    to_char(sale_order.date_order ,'DD-MM-YYYY') , sum(sale_order.amount_total) as amount

from 
    public.sale_order ,
    public.sale_order_line , 
    public.product_product ,
    public.product_template ,
    public.product_category

where
    sale_order_line.product_id = product_product.id AND
  product_product.product_tmpl_id = product_template.id AND
  product_template.categ_id = product_category.id AND
  sale_order.id = sale_order_line.order_id AND
  sale_order_line.product_id = product_product.id AND 
  product_product.product_tmpl_id = product_template.id AND 
  product_template.categ_id = product_category.id AND
  product_category.name = 'Starchi' and
    sale_order.order_year = '2015' and
  sale_order.order_month = 'April'
group by to_char(sale_order.date_order ,'DD-MM-YYYY') order by to_char(sale_order.date_order ,'DD-MM-YYYY') asc

然后它给出不同的输出

  to_char         amount
"14-04-2015";    1298.00
"15-04-2015";    4294.00
"16-04-2015";    1398.00
"17-04-2015";    2805.00     //wrong output
"18-04-2015";    6188.00    //wrong output
"19-04-2015";    13976.00  //wrong output
"20-04-2015";    19229.00  //wrong output
"21-04-2015";    3045.00

请问具体的问题是什么?

以及如何解决?

【问题讨论】:

  • 问题是你的连接增加了行数。 Postgres 工作正常。问题要么是你的数据,要么是你对数据的理解。
  • 我该如何解决这个问题,它可能会添加一行 sale_order 和 sale_order_line
  • 我首先将查询重写为连接语法。然后:注释掉所有未使用的表(特别是:order_line)。另外:不需要在 to_char() 上进行聚合,原始 date_order 上的聚合也可以正常工作。

标签: sql postgresql postgresql-9.2 odoo pgadmin


【解决方案1】:

正如 Gordon Linoff 已经评论的那样,您在 WHERE 子句中犯了一个错误,导致检索到重复的行,然后汇总得出错误的总数。按照 wildplasser 在他的评论中的建议,使用 JOIN 语法更加简洁且不易出错。您的查询将如下所示:

SELECT to_char(o.date_order, 'DD-MM-YYYY') AS "date", sum(o.amount_total) AS amount
FROM public.sale_order o
JOIN public.sale_order_line l ON l.order_id = o.id
JOIN public.product_product pp ON pp.id = l.product_id
JOIN public.product_template pt ON pt.id = pp.product_tmpl_id
JOIN public.product_category pc ON pc.id = pt.categ_id
WHERE pc.name = 'Starchi'
  AND o.order_year = '2015'
  AND o.order_month = 'April'
GROUP BY o.date_order
ORDER BY o.date_order ASC

【讨论】:

  • 它也给出与上面相同的输出定义没有解决方案
  • 那是不可能的。仔细查看 4 月 17 日至 20 日的数据,因为那里存在问题。编辑您的问题以发布其中 1 天或更多天的数据以获得更好的答案。
【解决方案2】:

[使用 @Patrick 的清理代码] 最可能的原因是订单和 order_lines 之间存在 1:N 关系,导致具有多个 order_lines 的订单多次包含在 sum() 中。

SELECT to_char(o.date_order, 'DD-MM-YYYY') AS date_order
        , sum(o.amount_total) AS amount
FROM public.sale_order o
WHERE EXISTS ( SELECT *
        -- These tables probably have an N:1 relation
        -- to public.sale_order
        -- We don't have to sum them; just check existence.
        FROM public.sale_order_line l
        JOIN public.product_product pp ON pp.id = l.product_id
        JOIN public.product_template pt ON pt.id = pp.product_tmpl_id
        JOIN public.product_category pc ON pc.id = pt.categ_id
        WHERE pc.name = 'Starchi'
        AND l.order_id = o.id
        )
  AND o.order_year = '2015'
  AND o.order_month = 'April'
GROUP BY o.date_order
ORDER BY o.date_order ASC
        ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    相关资源
    最近更新 更多