SQL Server Group By with Joins

时间:2017-03-29 18:37:02

标签: sql sql-server group-by sum

SELECT 
    [CustomerKey] AS 'Cust #',
    CU.[CompanyName] AS 'Company Name',
    ISS.InvoiceDate AS 'Invoice Date',
    ISS.InvoiceTotal AS 'Invoice Total',
    ISNULL(CU.ShopPhone,'') AS 'Company Shop',
    ISNULL(CU.CellPhone,'') AS 'Company Cell',
    ISNULL(CU.OfficePhone,'') AS 'Company Office',
    ISNULL(CF.FirstName, '') AS 'FName',
    ISNULL(CF.LastName,'') AS 'LName',
    ISNULL(CF.WorkPhone,'') AS 'Contact Work',
    ISNULL(CF.CellPhone,'') AS 'Contact Cell',
    ISNULL(CF.HomePhone,'') AS 'Contact Home',
    ISNULL(CF.EMail,'') AS 'Contact Email',
    PSO.OutsidePartsSalespersonName
FROM 
    [ProfitMaster].[dbo].[vwAC_SSR_Customer] CU with (nolock)
LEFT JOIN 
    [ProfitMaster].[dbo].[vwAC_SSR_InvoiceSalesSummary] ISS with (nolock) ON CU.CustomerKey = ISS.Customer
JOIN 
    [ProfitMaster].[dbo].[vwSV_INV_PartsSalesOrder] PSO with (nolock) ON PSO.PartsSalesOrderInvoiceID = ISS.PartsSalesOrderInvoiceID
LEFT JOIN
    (SELECT 
         EntityID, FirstName, LastName, WorkPhone, CellPhone, HomePhone, EMail 
     FROM 
         [ProfitMaster].[dbo].[vwGB_CON_ContactFull] with (nolock)  
     WHERE 
         EntityID IS NOT NULL 
         AND FirstName <> '' 
         AND EntityTypeID = '3' 
         AND SetDefault = '1'
     GROUP BY 
         EntityID, FirstName, LastName, WorkPhone, CellPhone, HomePhone, EMail) AS CF ON CU.CustomerID = CF.EntityID
WHERE 
    CU.Inactive = '0' 
    AND ISS.InvoiceType = 'Parts Order'
    AND ISS.InvoiceDate BETWEEN '2017-02-01 00:00:00.000' AND '2017-03-31 3:59:59.000'
    AND CU.CustomerBaseBranchID = '1'
    AND PSO.OutsidePartsSalespersonName IN ('Dave Freeland', 'Mark Miller', 'Ryan Oaks')
GROUP BY 
    CU.CustomerKey, CU.[CompanyName],
    ISS.InvoiceDate, ISS.InvoiceTotal, 
    CU.ShopPhone, CU.CellPhone, CU.OfficePhone, 
    CF.FirstName, CF.LastName, CF.WorkPhone, CF.CellPhone, 
    CF.HomePhone, CF.EMail, PSO.OutsidePartsSalespersonName
ORDER BY 
    CU.CompanyName, ISS.InvoiceDate

如何将此分组为按CustomerKey分组的SUM ISS.InvoiceTotal?

我一直认为&#34;在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。&#34;错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您在ISS表中SUM(),则无法从中进行选择或将其添加到GROUP BY

SELECT 
    [CustomerKey] AS 'Cust #',
    CU.[CompanyName] AS 'Company Name',
    LAST(ISS.InvoiceDate) AS 'Invoice Date',
    SUM(ISS.InvoiceTotal) AS 'Invoice Total',
    ISNULL(CU.ShopPhone,'') AS 'Company Shop',
    ISNULL(CU.CellPhone,'') AS 'Company Cell',
    ISNULL(CU.OfficePhone,'') AS 'Company Office',
    ISNULL(CF.FirstName, '') AS 'FName',
    ISNULL(CF.LastName,'') AS 'LName',
    ISNULL(CF.WorkPhone,'') AS 'Contact Work',
    ISNULL(CF.CellPhone,'') AS 'Contact Cell',
    ISNULL(CF.HomePhone,'') AS 'Contact Home',
    ISNULL(CF.EMail,'') AS 'Contact Email',
    PSO.OutsidePartsSalespersonName
FROM 
    [ProfitMaster].[dbo].[vwAC_SSR_Customer] CU with (nolock)
LEFT JOIN 
    [ProfitMaster].[dbo].[vwAC_SSR_InvoiceSalesSummary] ISS with (nolock) ON CU.CustomerKey = ISS.Customer
JOIN 
    [ProfitMaster].[dbo].[vwSV_INV_PartsSalesOrder] PSO with (nolock) ON PSO.PartsSalesOrderInvoiceID = ISS.PartsSalesOrderInvoiceID
LEFT JOIN
    (SELECT 
         EntityID, FirstName, LastName, WorkPhone, CellPhone, HomePhone, EMail 
     FROM 
         [ProfitMaster].[dbo].[vwGB_CON_ContactFull] with (nolock)  
     WHERE 
         EntityID IS NOT NULL 
         AND FirstName <> '' 
         AND EntityTypeID = '3' 
         AND SetDefault = '1'
     GROUP BY 
         EntityID, FirstName, LastName, WorkPhone, CellPhone, HomePhone, EMail) AS CF ON CU.CustomerID = CF.EntityID
WHERE 
    CU.Inactive = '0' 
    AND ISS.InvoiceType = 'Parts Order'
    AND ISS.InvoiceDate BETWEEN '2017-02-01 00:00:00.000' AND '2017-03-31 3:59:59.000'
    AND CU.CustomerBaseBranchID = '1'
    AND PSO.OutsidePartsSalespersonName IN ('Dave Freeland', 'Mark Miller', 'Ryan Oaks')
GROUP BY 
    CU.CustomerKey, CU.[CompanyName],
    CU.ShopPhone, CU.CellPhone, CU.OfficePhone, 
    CF.FirstName, CF.LastName, CF.WorkPhone, CF.CellPhone, 
    CF.HomePhone, CF.EMail, PSO.OutsidePartsSalespersonName
ORDER BY 
    CU.CompanyName