Sql Sum乘以结果

时间:2014-08-22 18:04:04

标签: sql sql-server sql-server-2008 sum inner-join

我试图将此转换为"毛利润"输入报告并遇到问题。

select 
   CONVERT(VARCHAR(12), ih.invoice_date,110) as invoice_date, 
   oh.order_no, 
   bosr.salesrep_name, 
   bosr.salesrep_id,
   oh.location_id, 
   oh.taker, 
   oh.customer_id, 
   Replace(oh.ship2_name, ',', ' ') as Ship_to_name,
   bosr.supplier_id, 
   Replace(bosr.supplier_name, ',', ' ') as Supplier_name,
   Cast((dc.dealer_commission_amt_due) as DECIMAL (19,2)) as "Gross Profit"
from oe_hdr oh 
 inner join anspin_view_booked_orders_ship_to_rep bosr
   on oh.order_no = bosr.order_no
 inner join oe_line ol 
   on oh.order_no = ol.order_no
 inner join invoice_hdr ih 
   on oh.order_no = ih.order_no
 inner join dealer_commission dc 
   on ih.invoice_no = dc.invoice_no
where 
  ih.invoice_date >= '2014-07-01' and
  ih.invoice_date < '2014-08-01' and
  ol.qty_ordered > '0' and
  bosr.source_code_no <> '706' and
  bosr.source_code_no <> '709'
group by 
  CONVERT(VARCHAR(12), ih.invoice_date, 110),
  oh.order_no, 
  bosr.salesrep_name, 
  bosr.salesrep_id,
  oh.location_id, 
  oh.customer_id, 
  oh.taker, 
  oh.ship2_name,
  bosr.supplier_id, 
  bosr.supplier_name, 
  dc.dealer_commission_amt_due
order by invoice_date;

这很有效,而且&#34;毛利润&#34;列显示我想要的日期范围中的正确值...现在,如果我要删除&#34; dc.dealer_commission_amt_due&#34;从小组中,然后进行此更改:

 Cast(sum(dc.dealer_commission_amt_due) as DECIMAL (19,2)) as "Gross Profit"

我的发票号码是SOME(不是全部!这很奇怪,因为有些是正确的)发票号码是原始值的2-4倍。

两者之间的例子:

invoice_date order_no salesrep_name salesrep_id location_id taker   customer_id Ship_to_name    supplier_id Supplier_name   Gross Profit
 07-10-2014   X         NAME          ID           60       NAME    X           Customer INC    123452      supplier INC.   4800.00

非萨姆:

invoice_date order_no salesrep_name salesrep_id location_id taker   customer_id Ship_to_name    supplier_id Supplier_name   Gross Profit
 07-10-2014   X         NAME          ID           60       NAME    X           Customer INC    123452      supplier INC.   750.00
invoice_date order_no salesrep_name salesrep_id location_id taker   customer_id Ship_to_name    supplier_id Supplier_name   Gross Profit
 07-10-2014   X         NAME          ID           60       NAME    X           Customer INC    123452      supplier INC.   450.00

根据我的阅读,这与联接有关......这是正确的吗?

3 个答案:

答案 0 :(得分:0)

这两个查询不一样:

SELECT Cast(( dc.dealer_commission_amt_due ) AS DECIMAL (19, 2)) AS  "Gross Profit" 
FROM  dealer_commission dc 
GROUP BY  dc.dealer_commission_amt_due 


SELECT Cast(( SUM(dc.dealer_commission_amt_due) ) AS DECIMAL (19, 2)) AS "Gross Profit" 
FROM  dealer_commission dc 

在GROUP BY子句中添加其他列将在结果中返回更多行,但不应影响总和。删除GROUP BY中的列将返回较少的行,并且不应再次影响总和。

查询中唯一可以影响总和的部分是哪些行匹配。

另外,请记住查询的操作顺序:

FROM  
WHERE  
GROUP BY  
HAVING  
SELECT  
ORDER BY

答案 1 :(得分:0)

您需要聚合吗?

select 
   CONVERT(VARCHAR(12), ih.invoice_date,110) as invoice_date, 
   oh.order_no, 
   bosr.salesrep_name, 
   bosr.salesrep_id,
   oh.location_id, 
   oh.taker, 
   oh.customer_id, 
   Replace(oh.ship2_name, ',', ' ') as Ship_to_name,
   bosr.supplier_id, 
   Replace(bosr.supplier_name, ',', ' ') as Supplier_name,
   SUM(Cast((dc.dealer_commission_amt_due) as DECIMAL (19,2))) as "Gross Profit"  --<<AGGREGATION ADDED
from oe_hdr oh 
 inner join anspin_view_booked_orders_ship_to_rep bosr
   on oh.order_no = bosr.order_no
 inner join oe_line ol 
   on oh.order_no = ol.order_no
 inner join invoice_hdr ih 
   on oh.order_no = ih.order_no
 inner join dealer_commission dc 
   on ih.invoice_no = dc.invoice_no
where 
  ih.invoice_date >= '2014-07-01' and
  ih.invoice_date < '2014-08-01' and
  ol.qty_ordered > '0' and
  bosr.source_code_no <> '706' and
  bosr.source_code_no <> '709'
group by 
  CONVERT(VARCHAR(12), ih.invoice_date, 110),
  oh.order_no, 
  bosr.salesrep_name, 
  bosr.salesrep_id,
  oh.location_id, 
  oh.customer_id, 
  oh.taker, 
  oh.ship2_name,
  bosr.supplier_id, 
  bosr.supplier_name
order by invoice_date;

答案 2 :(得分:0)

因为他们拥有&#34;百科全书&#34;所以必须支付软件公司的费用来写一个查询。把不同的表连在一起。

我能够通过自己摆脱乘法获得50%,但只有50%的订单类型出现了......我放弃了,现在有一个可以比较的工作查询。

感谢您的所有帮助和建议!