Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在postgresql中连接两个以上的表?_Sql_Postgresql_Postgresql 9.2_Odoo_Pgadmin - Fatal编程技术网

如何在postgresql中连接两个以上的表?

如何在postgresql中连接两个以上的表?,sql,postgresql,postgresql-9.2,odoo,pgadmin,Sql,Postgresql,Postgresql 9.2,Odoo,Pgadmin,我正在运行以下查询 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.d

我正在运行以下查询

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
到底是什么问题,谁都知道


如何解决呢?

正如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

[使用@Patrick的清理代码]最可能的原因是订单和订单行之间存在1:N关系,导致具有多个订单行的订单多次包含在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
        ;

问题是联接的行数是相乘的。博士后工作得很好。问题在于您的数据或您对数据的理解。如何解决此问题可能是添加销售订单行和销售订单行。我将首先重写查询以联接语法。然后:注释掉所有未使用的表(特别是:
order\u line
)。另外:不需要在
上聚合到\u char()
,在原始日期\u订单上聚合也可以。它还提供与上面相同的输出定义不可能的解决方案。请仔细查看您4月17日至20日的数据,因为存在一个问题。编辑您的问题,发布这些天中1天或以上的数据,以获得更好的答案。