Question

I have a table that contains hours for jobs technicians perform. The majority of the entries are positive but occasionally there is a credit to the customer that show as negative hours. My query includes sum(Amount) but only sums the positive numbers. It should sum 2 + 2 - 4 as 0, but it's giving me a total of 4.

Example of table

job no.    Amount
  j211      2
  j211      2
  j211     -4

select job no. sum(Amount)
from service.jobs
where job no. = 'j211'
group by job no.

This returns a total Amount for j211 of 4, not 0.

Was it helpful?

Solution

how about this one. try this one. and compare your table with this one.

CREATE TABLE [dbo].[service](
[jobno] [varchar](5) NULL,
[amount] [smallint] NULL
) ON [PRIMARY]

GO

Insert into service(jobno,amount) 
Select 'j211',2 UNION ALL
Select 'j211',2 UNION ALL
Select 'j211',-4

select sum(Amount)
from service
where jobno = 'j211'
group by jobno
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top