SQL - 加入多个表

时间:2014-10-21 06:09:16

标签: sql sql-server join

我有四个表CustomerSalesInvoiceReceipt

客户

ID      Name
1         A

销售

ID     Name
1      Ben

发票

ID     Amt      Date         CustomerID       SalesID
1      12       1/9/2014       1                 1
2      10       1/10/2014      1                 1
3      20       2/10/2014      1                 1
4      30       3/10/2014      1                 1

收据

ID     Amt      Date         CustomerID       SalesID
1      10       4/10/2014        1               1

我希望加入下面的4个表格,总结Ammount(s),但我仍然坚持如何实现我想要的

RESULT

CustomerID         SalesID         Inv_Amt       Rep_Amt       Month  
   1                  1               12            0            9
   1                  1               60            10           10

我已经被困了好几天了。但是,我不知道如何继续。

3 个答案:

答案 0 :(得分:1)

坦率地说,由于您只是选择客户和销售ID(而不是名称),您甚至不需要联合所有四个表:

SELECT   i.CustomerID, 
         i.SalesID, 
         SUM(i.Amt) AS InvAmt, 
         SUM(r.Amt) AS RepAmt, 
         MONTH(i.`Date`) AS `Month`
FROM     Invoice i
JOIN     Receipt r ON i.CustomerID = r.CustomerID AND 
                      i.SalesID = r.SalesID AND
                      MONTH(i.`Date`) = MONTH(r.`Date`)
GROUP BY i.CustomerID, i.SalesID, MONTH(i.`Date`) AS `Month`

答案 1 :(得分:1)

您可以通过分组和子查询获得月份总收据和发票金额,如下所示:

SELECT  Invoice.CustomerID [CustomerID], 
        Invoice.SalesID [SalesID], 
        SUM(Invoice.Amt) [Invoice_Amt], 
        ISNULL((SELECT SUM(Amt) 
                    FROM Receipt 
                    WHERE CustomerID = Invoice.CustomerID 
                    AND SalesID = Invoice.SalesID 
                    AND Month(Date) = Month(Invoice.Date)),0) [Receipt_Amt],
        MONTH(Invoice.Date) Month
FROM    Invoice
GROUP BY Invoice.CustomerID, Invoice.SalesID, MONTH(Invoice.Date)

<强> SQL Fiddle Demo1

警告:此处的数据将来自“发票”表格中的所有月份。如果任何月份,发票表中没有任何数据,那么即使收到也不会有该月的结果。

<强>更新 要从发票和收据表的所有月份获得结果,您需要使用CTE获取它,如下所示:

;with CTE as
(
    SELECT Invoice.CustomerID, Invoice.SalesID, MONTH(Invoice.Date) MonthNo FROM Invoice
    UNION
    SELECT Receipt.CustomerID, Receipt.SalesID, MONTH(Receipt.Date) MonthNo FROM Receipt
)


SELECT  CTE.CustomerID [CustomerID], 
        CTE.SalesID [SalesID], 
        ISNULL((SELECT SUM(Amt) 
                    FROM Invoice
                    WHERE CustomerID = CTE.CustomerID 
                    AND SalesID = CTE.SalesID 
                    AND Month(Date) = CTE.MonthNo),0) [Invoice_Amt],
        ISNULL((SELECT SUM(Amt) 
                    FROM Receipt 
                    WHERE CustomerID = CTE.CustomerID 
                    AND SalesID = CTE.SalesID 
                    AND Month(Date) = CTE.MonthNo),0) [Receipt_Amt],
        MonthNo
FROM    CTE

<强> SQL Fiddle Demo2

答案 2 :(得分:0)

看起来像是作业,但是......

SELECT 
  Customer.ID AS CustomerID, 
  Sales.ID AS SalesID, 
  Invoice.Amt AS Inv_Amt, 
  Receipt.Amt AS Rep_Amt, 
  MONTH(Invoice.Date) AS Month 
FROM 
  Customer 
  INNER JOIN Receipt ON Customer.ID = Receipt.CustomerID 
  INNER JOIN Invoice ON Customer.ID = Invoice.CustomerID 
  INNER JOIN Sales ON Sales.ID = Receipt.SalesID

我没有费心检查结果是否符合您的预期,但查询应该是这样的。您可以使用连接条件来获得结果。