Add my sales together based on Id and Name using SUM function

孤者浪人 提交于 2020-03-05 03:08:12

问题


I created a request with SSMS:

SELECT 
    CONCAT(LastName,' ', FirstName) AS [Nom du vendeur], 
    SalesPersonID, 
    DATEPART(YYYY, [OrderDate]) AS [Annee], 
    FORMAT(SUM(soh.SubTotal), '#,#00.') AS [Ventes]
FROM 
    Sales.SalesOrderHeader AS soh 
INNER JOIN 
    Person.Person AS pp ON soh.SalesPersonID = pp.BusinessEntityID
WHERE 
    pp.PersonType = 'SP' 
    AND soh.OnlineOrderFlag = '0' 
    AND OrderDate NOT BETWEEN CONVERT(DATETIME, '01/01/2011', 101) AND CONVERT(DATETIME, '12/31/2011', 101) 
GROUP BY 
    SubTotal, OrderDate, SalesPersonID,LastName, FirstName
ORDER BY  
    [Annee], [Nom du vendeur]

And here is my output:

As you can see in the [ventes] column which represent the sales column, the sales don't add up depending on the Id number and Name but I do use the SUM() function in my select. Any ideas as to what is wrong with my code?


回答1:


As mentioned in the comments,

remove subtotal from the group by clause, aggregated values don't need to be in the group by clause.




回答2:


The issue is your group by:

  1. You don't want to group by SubTotal when you are summing that column.
  2. And you don't want to group by OrderDate when you are trying to sum the totals over a year. Rather you want to group by the same computation you are selecting e.g. DATEPART(YYYY, [OrderDate]).

So your corrected group by is:

GROUP BY DATEPART(YYYY, [OrderDate]), SalesPersonID, LastName, FirstName

FYI: If you post your questions in a format similar to the following, which is a Minimal Reproducible Example you make it much easier and much more likely for people to assist.

declare @SalesOrderHeader table (id int, SalesPersonID int, OrderDate datetime, SubTotal money, OnlineOrderFlag varchar(1))
declare @Person table (id int, FirstName varchar(64), LastName varchar(64), BusinessEntityID int, PersonType varchar(2))

insert into @Person (id, BusinessEntityID, FirstName, LastName)
  select 1, 1, 'Amy', 'Alberts' union all
  select 2, 2, 'Pamela', 'Ansman-Wolfe'

insert into @SalesOrderHeader (SalesPersonID, OrderDate, SubTotal)
  select 1, '5 nov 2019', 12.34 union all
  select 1, '6 nov 2019', 34.56 union all
  select 2, '7 nov 2019', 78.90 union all
  select 2, '8 nov 2019', 43.21

SELECT 
  CONCAT(LastName,' ', FirstName) AS [Nom du vendeur] 
  , SalesPersonID 
  , DATEPART(YYYY, [OrderDate]) AS [Annee] 
  , FORMAT(SUM(soh.SubTotal), '#,#00.') AS [Ventes]
FROM @SalesOrderHeader AS soh 
INNER JOIN @Person AS pp ON soh.SalesPersonID = pp.BusinessEntityID
--WHERE 
--    pp.PersonType = 'SP' 
--    AND soh.OnlineOrderFlag = '0' 
--    AND OrderDate NOT BETWEEN CONVERT(DATETIME, '01/01/2011', 101) AND CONVERT(DATETIME, '12/31/2011', 101) 
-- GROUP BY SubTotal, OrderDate, SalesPersonID,LastName, FirstName
GROUP BY DATEPART(YYYY, [OrderDate]), SalesPersonID, LastName, FirstName
ORDER BY [Annee], [Nom du vendeur]

Returns:

Nom du vendeur      | SalesPersonID | Annee | Ventes
----------------------------------------------------
Alberts Amy         | 1             | 2019  | 47
Ansman-Wolfe Pamela | 2             | 2019  | 122


来源:https://stackoverflow.com/questions/58719775/add-my-sales-together-based-on-id-and-name-using-sum-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!