加入sql查询的问题

时间:2013-12-30 07:16:59

标签: sql sql-server-2008 select group-by sum

我们正在使用shipworks应用程序来管理我们的订单。有了这个,我们还开发了一个小工具,可以帮助根据船舶数据生成不同类型的报告。

因此,我们在为客户生成销售报告时面临的问题很少。在本报告中,我们将获取以下信息:
1.客户ID,BillFirstName,BillMiddleName,BillLastName,OrderTotal为Order表中的总计。 2.来自OrderItem表的SalePrice。 Saleprice是(数量* unitprice)。 OrderItem表以特定顺序保存订购数量和单价的产品。我们通过OrderID在Order和OrderItem表之间建立了关系。 3.来自OrderCharge表的折扣,税和运费。折扣是具有相应类型列值(COUPON,PROMOTION DISCOUNT,REWARD,CREDIT,PROMOTION,FACEBOOK_FAN_REWARD,VOUCHER)的所有金额的总和,而Taxes是具有相应类型列值(TAX)并且运费为总和的金额之和具有相应类型列值的数量(SHIPPING)。我们通过OrderID在OrderCharge和Order表之间建立了关系。

OverAll,我们针对每个customerID获取上述数据。我们还通过一些商店条件和日期条件来获取数据,即我们通过时订单属于商店,属于通过的开始和结束日期。

我们已将上述查询作为:

select derived.CustomerID, derived.BillFirstName, derived.BillMiddleName,   
derived.BillLastName, SUM(oi.quantity*oi.UnitPrice) as saleprice, derived.freight,   
derived.tax, sum(o.OrderTotal) as total, derived.discount 
from (select o.CustomerID, o.BillFirstName, o.BillMiddleName, o.BillLastName
,SUM(oc.amount) as freight, SUM(oc2.amount) as tax, SUM(oc3.amount) as discount 
from [Order] o   
inner join OrderCharge oc on o.orderID = oc.orderID 
                         and oc.[Type] = 'SHIPPING'  
inner join OrderCharge oc2 on o.orderID = oc2.orderID 
                         and oc2.Type = 'TAX'  
inner join OrderCharge oc3 on o.orderID = oc3.orderID 
                         and (oc3.Type = 'COUPON' 
                              or oc3.Type = 'PROMOTION DISCOUNT' 
                              or oc3.Type = 'REWARD' 
                              or oc3.Type = 'CREDIT' 
                              or oc3.Type = 'PROMOTION' 
                              or oc3.Type = 'FACEBOOK_FAN_REWARD' 
                              or oc3.Type = 'VOUCHER')  
where o.StoreID IN(12005,17005,1005,20005,19005) 
and (o.OrderDate between '2013-11-01 00:00:00' and '2013-12-27 23:59:00') 

group by o.CustomerID, o.BillFirstName,  
o.BillMiddleName, o.BillLastName) as derived 

inner join [Order] o on o.CustomerID =  derived.CustomerID 

inner join orderItem oi on o.orderID = oi.orderID  

group by derived.CustomerID, derived.BillFirstName, derived.BillMiddleName,  
derived.BillLastName, derived.freight, derived.tax, derived.discount  

以上查询为我们提供了正确的数据,但未对折扣,运费和税金进行总结。任何人都可以建议我其他解决方案或告诉我上述查询有什么问题?

等待回复

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT o.CustomerID, o.BillFirstName, o.BillMiddleName, o.BillLastName, 
       SUM(B.saleprice) AS saleprice, SUM(A.freight) AS freight, 
       SUM(A.tax) AS tax, SUM(o.OrderTotal) AS total, SUM(A.discount) AS discount
FROM ORDER o   
INNER JOIN (SELECT orderID, 
                   SUM(CASE WHEN oc.TYPE = 'SHIPPING' THEN oc.amount ELSE 0 END) AS freight,  
                   SUM(CASE WHEN oc.TYPE = 'TAX' THEN oc.amount ELSE 0 END) AS tax, 
                   SUM(CASE WHEN oc.TYPE IN ('COUPON', 'PROMOTION DISCOUNT', 'REWARD', 'CREDIT', 'PROMOTION', 'FACEBOOK_FAN_REWARD', 'VOUCHER') THEN oc.amount ELSE 0 END) AS discount
            FROM OrderCharge oc GROUP BY oc.orderID 
           ) A ON o.orderID = A.orderID 
INNER JOIN (SELECT orderID, SUM(oi.quantity * oi.UnitPrice) AS saleprice 
            FROM OrderItem oi GROUP BY oi.orderID
           ) B ON o.orderID = B.orderID  
WHERE o.StoreID IN(12005,17005,1005,20005,19005) AND 
      o.OrderDate BETWEEN '2013-11-01 00:00:00' AND '2013-12-27 23:59:00'
GROUP BY o.CustomerID, o.BillFirstName, o.BillMiddleName, o.BillLastName