0

I have the following table called designerindices

enter image description here

I want to do the sum and the pseudo code as below

SUM(duration1) WHERE designer1 = X AND designerHistSrNumber = 16 + SUM(duration2) WHERE designer2 = X AND designerHistSrNumber = 16 + SUM(duration3) WHERE designer3 = X AND designerHistSrNumber = 16 + SUM(duration4) WHERE designer 4 = X AND designerHistSrNumber = 16

To do that I wrote the following 4 separate queries

SELECT SUM(duration1) as sumdur1 FROM designerindices WHERE designerHistSrNumber = 16 AND designer1 = "X" This should output 10

SELECT SUM(duration2) as sumdur2 FROM designerindices WHERE designerHistSrNumber = 16 AND designer2 = "X" This should output 2.4

SELECT SUM(duration3) as sumdur3 FROM designerindices WHERE designerHistSrNumber = 16 AND designer3 = "X" This should output 5

SELECT SUM(duration4) as sumdur4 FROM designerindices WHERE designerHistSrNumber = 16 AND designer4 = "X" This should output 1.1

I have to execute the above queries 4 times. The finally add sumdur1 + sumdur2 + sumdur3 + sumdur4. The total should be 18.5

Is there any easy and direct way to do this instead of doing the above way?

1

3 Answers 3

2

You can wrap all your 4 queries in seperate Select clause and have addition in that, like

Select (
(SELECT SUM(duration1) as sumdur1 FROM designerindices WHERE designerHistSrNumber = 16 AND designer1 = "X")
+
(SELECT SUM(duration2) as sumdur2 FROM designerindices WHERE designerHistSrNumber = 16 AND designer1 = "X")
+
(SELECT SUM(duration3) as sumdur3 FROM designerindices WHERE designerHistSrNumber = 16 AND designer1 = "X")
+
(SELECT SUM(duration4) as sumdur4 FROM designerindices WHERE designerHistSrNumber = 16 AND designer1 = "X")
) from Dual;
Sign up to request clarification or add additional context in comments.

2 Comments

This works well too. But if use multiple select cases, will it be slow when comes to large tables?
@Anu The downside of this approach is that it requires four separate full scans of the designerindices table, whereas using a single select with conditional aggregation only requires one scan.
1

What you want here generally is called conditional aggregation:

SELECT
    SUM(CASE WHEN designer1 = 'X' THEN duration1 ELSE 0 END) AS dur1,
    SUM(CASE WHEN designer2 = 'X' THEN duration2 ELSE 0 END) AS dur2,
    SUM(CASE WHEN designer3 = 'X' THEN duration3 ELSE 0 END) AS dur3,
    SUM(CASE WHEN designer4 = 'X' THEN duration4 ELSE 0 END) AS dur4
FROM yourTable
WHERE
    designerHistSrNumber = 16

7 Comments

seems like it is not doing SUM. When I run this query, it is just getting the first value of each duration column only. Eg: dur1 showing 10, dur2 showing 2.4, dur3 showing 5 and dur4 showing 1.1.
@Anu According to your sample data, there is only one record where designer1='X' and designerHistSrNumber=16. Same for the other SUMs: there is only one matching record for each of those conditions. That explains why dur1 = 10, dur2 = 2.4, etc.
@kmoser Sorry my bad. You are right. Your suggestion is correct.
Just add together the four CASE expressions in my answer. If you find that too ugly, then you could subquery my answer and then just sum the aliases.
@Anu Unless your DB is enormous and/or poorly indexed, I suspect the performance differences between the two methods will be negligible. But don't take my word for it: perform some tests and let us know the results!
|
0

Why not just select the individual SUMS:

SELECT
  SUM(duration),
  SUM(duration2),
  SUM(duration3),
  SUM(duration4)
FROM
  designerindices
WHERE
  designerHistSrNumber=16 AND designer1="X"

This will sum the individual columns, but only for rows that match the WHERE clause. Since you want the same WHERE clause for each SUM(), you can just specify it once.

5 Comments

...because the restriction isn't always on designer1, it is also on designer2, designer3, etc.
I was basing my query on OP's individual queries, all of which are dependent on designer1. So perhaps the original post needs to be clarified.
@kmoser Sorry that was a typo mistake from me. designer must vary for each query
I just corrected it
@kmoser Please read the above 2 OP comments, which confirm my initial comment under your answer.

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.