Using same table in a UNION multiple times I am given a query that is using the same table with same columns and filters multiple times and the only thing is changing is the Type which is given manually. I am thinking there must be a better way to do that.
Query statement is like below:
    Select Column 1, Column 2, Sum(Column 3) AS Test, &quot;First Query&quot; AS &quot;Type&quot;
           From Table X
           Where Column 1 = &quot;Fly&quot;
    Group by Column 1, Column 2
    
    UNION ALL 
    
    Select Column 1, Column 2, Sum(Column 3) AS Test, &quot;Second Query&quot; AS &quot;Type&quot;
           From Table X
           Where Column 1 = &quot;Fly&quot;
    Group by Column 1, Column 2
 
    UNION ALL 
    
    Select Column 1, Column 2, Sum(Column 3) AS Test, &quot;Third Query&quot; AS &quot;Type&quot;
           From Table X
           Where Column 1 = &quot;Fly&quot;
    Group by Column 1, Column 2

Thanks!
Use
Select X.Column1, X.Column2, Sum(X.Column3) AS Test, Types.&quot;Type&quot;
From Table X
CROSS JOIN ( SELECT &quot;First Query&quot; AS &quot;Type&quot;
             UNION ALL
             SELECT &quot;Second Query&quot;
             UNION ALL
             SELECT &quot;Third Query&quot; ) AS Types
Where X.Column1 = &quot;Fly&quot;
Group by X.Column1, X.Column2, Types.&quot;Type&quot;


Maybe SELECTs in Types needs additional FROM DUAL or similar, or DB2 uses some another method for to select scalar value - test.
UPDATE (copied from the comment)

You can shorten the CROSS JOIN part with:
CROSS JOIN ( values ('First Query'),('Second Query'),('Third Query')) types (type)

FWIW, Db2 also allows
CROSS JOIN ( values 'First Query','Second Query','Third Query') types (type)

but that's non standard I beleive – Lennart