我正试图找到我的发票sql的总数

时间:2018-04-05 17:13:53

标签: sql

所以我有两张表invoice_w和items_sold_w。我试图将所售商品*每件商品的价格相乘,然后加上税,运费,折扣和安装费。代码运行但问题是它根据发票上有多少不同的项目来计算税金,折扣,运费和安装,因此如果发票有2个项目,它将向他们收取双重运费,税金,折扣和安装费用。 / p>

SELECT DISTINCT invoice_no, invoice_no(SUM((item_qty_sold*item_price)+invoice_tax+invoice_shipping-invoice_discount+invoice_installation_fee)) "Total Price"
FROM invoices_w JOIN
     items_sold_w
     USING (invoice_no)
GROUP BY invoice_no;

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

SELECT invoice_no,
       (SUM(item_qty_sold * item_price) + invoice_tax + invoice_shipping - invoice_discount + invoice_installation_fee) as Total_Price
FROM invoices_w JOIN
     items_sold_w
     USING (invoice_no)
GROUP BY invoice_no, invoice_tax, invoice_shipping, invoice_discount, invoice_installation_fee;

如果任何发票值为NULL,则您需要使用COALESCE()