JOIN子SELECT语句

时间:2014-12-29 10:58:38

标签: sql sql-server select join

我遇到了如何加入子选择语句的问题。

打算检索父/子层次结构中链接的所有销售额的总和。每一行应显示层次结构中总销售额的总和,而不是唯一的子销售额

我可以轻松地分别获得所需的结果,但试图找出如何在单个查询中获取此数据。

预期结果:

AccountID   AccountName       REPLOGIN     TL_AccountID   TL_AccountName     SumGross
123456      Child Company A   Salesrep A   123654         Parent Company X   1500
123456      Child Company B   Salesrep C   123654         Parent Company X   1500

下面的SQL。子选择结果必须与TL_AccountID:

连接
SELECT
    dbo.Account.AccountID,
    dbo.Account.AccountName,
    dbo_Rep_Employee.Login AS REPLOGIN,
    TL_Account.AccountID AS TL_AccountID, 
    TL_Account.AccountName AS TL_AccountName, 
    TL_Address.Country AS TL_Country,

------------------------------------------  
-- HOW TO JOIN THE BELOW????? --
------------------------------------------  

(SELECT SUM(Orders.GrossAmount) AS SumGross

FROM    Position 
                INNER JOIN Account ON Position.PositionID = Account.PositionID 
                INNER JOIN Employee ON Position.EmployeeID = Employee.EmployeeID 
                INNER JOIN Address ON Account.AddressID = Address.AddressID 
                INNER JOIN Account AS TL_Account ON Account.TopPurchasingAccountID = TL_Account.AccountID 
                INNER JOIN Address AS TL_Address ON TL_Account.AddressID = TL_Address.AddressID 
                RIGHT OUTER JOIN Orders AS Orders ON Account.AccountID = Orders.AccountID 
                LEFT OUTER JOIN PriceList AS PriceList ON Orders.PriceListID = PriceList.PriceListID

WHERE               
                (Orders.SalesChannel LIKE '%NL.%' OR Orders.SalesChannel LIKE '%.nl%') AND 
                (Orders.DateDeleted IS NULL) AND 
                (Orders.OrderDate > DATEADD(MM, - 13, CONVERT(DATE, DATEADD(mm, DATEDIFF(MM, 0, DATEADD(DD, - 3, CURRENT_TIMESTAMP)) + 1, 0)))) AND 
                (UPPER(RTRIM(Orders.OrderStatus)) IN ('VERIFIED', 'VERIFIED - MANUAL', 'SENT TO FINANCE', 'REJECTED', 'RE-SUBMITTED', 'PAID IN FULL', 'INVOICED - MANUAL', 'INVOICED', 'FINANCE HOLD', 'CLOSED--EU CREDIT', 'CLOSED - EU CREDIT', 'VERIFIED BY FINANCE', 'Pending Approval', 'Closed--Zero Value'))) AS SumGross
------------------------------------------  
-- HOW TO JOIN THE ABOVE????? --
------------------------------------------  


FROM
    dbo.Account INNER JOIN dbo.AccountPosition ON (dbo.Account.AccountID=dbo.AccountPosition.AccountID)
    INNER JOIN dbo.Position  dbo_Rep_Position ON (dbo.AccountPosition.PositionID=dbo_Rep_Position.PositionID)
    INNER JOIN dbo.Employee  dbo_Rep_Employee ON (dbo_Rep_Position.EmployeeID=dbo_Rep_Employee.EmployeeID)
    INNER JOIN dbo.Employee  dbo_Rep_Manager ON (dbo_Rep_Employee.ManagerEmployeeID=dbo_Rep_Manager.EmployeeID)
    INNER JOIN dbo.Employee  dbo_Modified_Employee ON (dbo_Modified_Employee.EmployeeID=dbo.AccountPosition.ModifyEmployeeID)
    LEFT JOIN Account AS TL_Account ON Account.TopPurchasingAccountID = TL_Account.AccountID 
    INNER JOIN Address AS TL_Address ON TL_Account.AddressID = TL_Address.AddressID 
    INNER JOIN  Orders ON Account.AccountID = Orders.AccountID

WHERE
    dbo.Account.AccountID  IN  ( N'1-9CLBQK') 
    AND
    dbo.AccountPosition.PrimarySalesRep  IN  ( N'Y'  )
    AND
    dbo.AccountPosition.DateDeleted  Is Null  

    GROUP BY
    dbo.AccountPosition.DateModified,
    dbo_Modified_Employee.login,
    dbo.Account.AccountID,
    dbo.Account.AccountName,
    dbo_Rep_Employee.Login,
    dbo.Account.DateCreated,
    TL_Account.AccountID, 
    TL_Account.AccountName,
    TL_Address.Country 

1 个答案:

答案 0 :(得分:0)

您可以尝试使用SQL用户定义函数,它将接受输入参数并返回 表值结果集,其中包含可以连接到上述结果集的查询下半部分的必需值