I've got a table that has multiple records of the identical telemarketer id but different sales amount. How do i remove the multiple rows and merely possess the aggregate from the total value.
Let's for instance assume the table structure the following -
SalesAgentId, SalesAgentName, SalesAmount 111 , John Doe , 8437.00 112 , John O Connor , 5849.00 111 , John Doe , 438.00 112 , John O Connor , 1234.00
Things I want is (even when it's right into a different table) the next -
SalesAgentId, SalesAgentName, SalesAmount 111 , John Doe , 8875.00 112 , John O Connor , 7083.00
Are we able to do that using SQL claims or if it is an Saved Procedure only? If it's utilizing an SP, do we must iterate through each one of the records...check if it's ever present, if that's the case, just add the SalesAmount area towards the existing table and implement it?
SELECT SalesAgentId, SalesAgentName, SUM(SalesAmount) AS SalesAmount INTO #AggSales FROM Sales GROUP BY SalesAgentId, SalesAgentName; TRUNCATE TABLE Sales; INSERT INTO Sales SELECT * FROM #AggSales; DROP TABLE #AggSales;
This is actually the simplest way I'm able to think about doing the work:
create table #tempSales (salesagentid int, salesagentname varchar(50), salesamount money) go insert into #tempSales select salesagentid, salesagentname, sum(salesamount) from salesTable group by salesagentid, salesagentname go select * from #tempSales
SELECT SalesAgentID, SUM(SalesAmount) FROM Sales GROUPBY SalesAgentID
There is however a problem here... Why your table have BOTH SalesAgentId and SalesAgentName?
It will contain just the ID, the title ought to be inside a SalesAgent table. You would then retrieve the title having a join