在SQL中查找客户总数

时间:2016-03-09 04:37:56

标签: mysql sql

我试图找出库存数据库中特定客户的总金额。 表格是: 客户,税,发票,line_no,分支,项目,员工

我能够计算总数,但我得到多个金额,因为它计算数据库中每个税率的金额。我试图根据分支机构的问题来限制金额。我希望我说清楚,这是查询:

SELECT  ROUND((SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY)* (TAX.TAX_RATE))+
SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY),2)
AS "TOTAL PAYMENT" 
FROM LINE_NO, TAX, BRANCH
WHERE LINE_NO.INVOICE_INVOICE_NO IN (SELECT INVOICE.INVOICE_NO from INVOICE
WHERE(INVOICE.CUSTOMER_CUST_NO IN (SELECT CUST_NO from CUSTOMER where CUSTOMER.FNAME='JIM' ))) 
AND TAX.CITY = BRANCH.CITY
GROUP BY TAX.TAX_RATE;

2 个答案:

答案 0 :(得分:0)

GROUP BY不应在此操作TAX_RATE以避免计算每组的总金额(TAX_RATE)。

SELECT  ROUND((SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY)* (TAX.TAX_RATE))+
SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY),2)
AS "TOTAL PAYMENT" 
FROM LINE_NO, TAX, BRANCH
WHERE LINE_NO.INVOICE_INVOICE_NO IN (SELECT INVOICE.INVOICE_NO from INVOICE
WHERE(INVOICE.CUSTOMER_CUST_NO IN (SELECT CUST_NO from CUSTOMER where CUSTOMER.FNAME='JIM' ))) 
AND TAX.CITY = BRANCH.CITY;

答案 1 :(得分:0)

准确。是的,可以从分支机构位置检索费率,可以通过发票编号进行跟踪。所以我在之后添加了另一个查询:

AND TAX.CITY = BRANCH.CITY

这有助于确定我正在寻找并使其正常运转的税率,无论如何,谢谢