0

Given table:

ID   ANOTHERID      ONE    TWO      THREE
X1       B1          15    15       -
X1       B2          10    -        - 
X2       B1          -     20       -

This query:

SELECT SUM (ONE + TWO + THREE) FROM (TABLE)
GROUP BY ID, ANOTHERID

I also tried

select sum(coalesce( ONE + TWO + THREE, ONE + TWO, ONE + THREE, ONE + 
THREE))

at least one column has a null value. How can I still add them even if there is a null? As null and 0 have different meanings here (null means not started, 0 means not worked), I dont want to replace null with 0. Thanks

6
  • Why is using zero in the sum a problem since it doesn't affect the result? What effect do you want the null to have that is different to treating it as zero? (Or do you just mean you don't want to change the value in the table to zero - as opposed to just replacing it within the query?) Commented Mar 5, 2018 at 23:09
  • Use nvl(one,0)+nvl(two,0,)+nvl(three,0), etc. Commented Mar 5, 2018 at 23:11
  • what do you expect the total to be when there is a NULL? Commented Mar 5, 2018 at 23:12
  • basically, the table is an employee table, ppl who havent worked in a week will have 0 and people who have recently hired but not worked (like they will start in few weeks) will have null. so, dont want to use "Use nvl(one,0)+nvl(two,0,)+nvl(three,0), etc".. Management wants to see list of people who havent worked (0 hours) and all new employees (null values) separately. Commented Mar 5, 2018 at 23:20
  • @Randy: it shows empty if one of them is empty but it should show null if all of them (ONE, TWO, THREE) are null. If one of them isnt null, then sum. the final sum query should return values, null, 0 separately. Commented Mar 5, 2018 at 23:24

4 Answers 4

1

One method is:

SELECT SUM(COALESCE(ONE, 0) + COALESCE(TWO, 0) + COALESCE(THREE, 0))
FROM (TABLE)
GROUP BY ID, ANOTHERID;

Or, if you have at least one non-NULL value in each column:

SELECT SUM(ONE) + SUM(TWO) + SUM(THREE)
Sign up to request clarification or add additional context in comments.

Comments

1

The time reporting table(s) should not allow null values, and the employee table should have a hire date field which can be used as criteria in your reporting queries. This will enable you to accurately report what management expects.

Comments

1

This solution worked for me

select 
case when coalesce(sum(ONE), sum(TWO), sum(THREE)) is null then null else 
sum(nvl(ONE,0) + nvl(TWO,0) + nvl(THREE,0)) end as 
TOTALSUM
GROUP BY ID, ANOTHERID; 

Comments

0

You might need to add another column in your table that describes the status of the employee (new, old) then make a condition like this:

if emp_status = 'new' then
    --some code
    working_hours := null;
else
     --some code
    working_hours : 0;
end if;

1 Comment

its a fairly big software..making changes to schema is outside of this work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.