Skip to content
Advertisement

Sum values in two different tables and join results keeping the columns

I have two tables: one with downtime and the other with productive time.

Table with productive time

Downtime

I want to have a table like this

enter image description here

But I am getting this

enter image description here

In the result, I am getting twice the downtime of the sum for the report 04102021-1, but as can be seen in the second picture, the value is present only once.
The script I am using is the following:

After the @xQbert comment, I tried this:

But I got the same result.
May you give some advice to get the result I want?
Thanks in advance

Advertisement

Answer

You could use conditional aggregation for this, but the easiest, and probably most performant, way to do this is to pre-aggregate the results before you join.

Note the use of CROSS APPLY (VALUES to avoid code repetition.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement