加入3个表格,根据不同的客户获取月度报告

时间:2015-08-17 14:19:20

标签: sql-server sql-server-2008

我有3张桌子。

1.Customers Table(CustomerID,CustomerName)
2.SalesTable(SalesChannel,CustoemrID,SalesID)
3.TransactionTable(SalesID,UnitsSold,TotalAmount,SellingDate)

现在我需要每个客户每个月的总销量和总销量? 请帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

SELECT C.CustomerID
      ,C.CustomerName
      ,MONTH(t.SellingDate) AS SalesMonth
      ,SUM(t.UnitsSold * t.TotalAmount) AS TotalSales
FROM       Customers C 
INNER JOIN SalesTable S       ON S.CustomerID = C.CustomerID
INNER JOIN TransactionTable t ON t.SalesID = S.SalesID
GROUP BY C.CustomerID ,C.CustomerName ,MONTH(t.SellingDate)