Skip to content
Advertisement

Apply column filter on summed columns in DB2

I have the following DB2 table:

COLUMN_NAME DATA_TYPE TYPE_NAME COLUMN_SIZE COLUMN_TEXT
DMPROD -2 CHAR () FOR BIT DATA 35 Product Code
DMPTYP -2 CHAR () FOR BIT DATA 1 Period Type
DMTYPE -2 CHAR () FOR BIT DATA 6 Type of Data
DMVL01 3 DECIMAL 17 Value Period 1
DMVL02 3 DECIMAL 17 Value Period 2
DMVL03 3 DECIMAL 17 Value Period 3
DMVL04 3 DECIMAL 17 Value Period 4
DMVL05 3 DECIMAL 17 Value Period 5
DMVL06 3 DECIMAL 17 Value Period 6
DMVL07 3 DECIMAL 17 Value Period 7
DMVL08 3 DECIMAL 17 Value Period 8
DMVL09 3 DECIMAL 17 Value Period 9
DMVL10 3 DECIMAL 17 Value Period 10
DMVL11 3 DECIMAL 17 Value Period 11
DMVL12 3 DECIMAL 17 Value Period 12
DMYEAR 3 DECIMAL 4 Fiscal Year

Below query would return the sum of value periods for each DMTYP:

Output is what I want, except value periods cannot easily be changed in this query without rewriting all sums. For example, say I only want to see Value Period 1-5 in DMYEAR 2022, I would need to change every case statement. Would there be an easier way, perhaps with subqueries, to keep the SUM statements the same but select only the desired value periods for the final output?

Advertisement

Answer

You could solve this using a subquery:

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