类似的查询,不同的总和

时间:2018-04-04 23:31:35

标签: sql

我正在尝试使用产品信息表加入订单表,这样我就可以创建一个包含所有订单的表格,这样我就可以按可用维度减少收入,但我运行的每个查询都有不同的总和

我的表看起来像这样:

订单表:id,日期,产品名称,订购数量    产品表:id,名称,类型,艺术家,sku,价格,可见性,产品类型

这些查询中的每一个都会产生不同的总数,我只想了解原因。

查询1:

select sum(subtotal) 
from (select orders.date, orders."product name", 
  orders."quantity ordered", products."product type", products.price,
  products.artist, ("quantity ordered"*price) AS "subtotal"
from orders
LEFT JOIN products ON orders."product name"=products.name)

查询2:

select sum(subtotal) 
from (select orders.date, orders."product name", 
  orders."quantity ordered", products."product type", products.price,
  products.artist, ("quantity ordered"*price) AS "subtotal"
from orders
LEFT JOIN products ON orders."product name"=products.name)
Group by orders.id, orders.date, orders."product name", orders."quantity 
         ordered", products."product type", products.price, products.artist, subtotal

当我导出两个表并以价格查找VLOOKUP的总和时,我会得到与这两个查询不同的答案。

假设查询正在创建重复项,但无法跟踪我的错误。

0 个答案:

没有答案