Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

problem with using Case statement and a variable - need to results of the case @variable to populate a new column

$
0
0

I am trying add up all the sales if the RepID matches the the specialtyrepid and add all those values to be the value for the column name SpecialtySalesRepforPRS.. example if the SalesRepID is 51 and udflumbersalesRepID is 51 then I want to take the value of LumberRepSales have that value. Here's the twist. This has to occur with all the specialtyrepsid that match and keep adding it up. so that the final value is the sum of all specialrepsales that match on primaryrepid

I hope that makes sense. I am trying to use @howmuch to be the bucket. Right now the original value @howmuch never gets increased. it stays at 0 and 0 is in the column name SpecialtySalesRepfor PRS

------- here's the code

declare @howmuch as decimal(12,2) = 0;
select InvYear,InvMonth, TotalCost,CustomerID,UserID,

SalesRepID,PrimaryRepSales, udfInsulationSalesRepID,InsulationRepSales,udfLumberSalesRepID,LumberRepSales,
udfMillworkSalesRepID,MillRepSales,udfTrussSalesRepID,TrussRepSales,

case
When (SalesRepID = isnull(udfInsulationSalesRepID,0))then @howmuch + InsulationRepSales

When (SalesRepID = isnull(udfLumberSalesRepID,0)) then @howmuch + LumberRepSales

When (SalesRepID = isnull(udfMillworkSalesRepID,0)) then @howmuch + MillRepSales

When (SalesRepID= isnull(udfTrussSalesRepID,0))then @howmuch + TrussRepSales

end

 [SpecialtyRepSalesforPRS]
 from SalesBySalesRepsDetail






Viewing all articles
Browse latest Browse all 23857

Trending Articles