IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

count contained values of several columns (in one table)


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Auryn

New User


Joined: 11 Jan 2006
Posts: 88
Location: Lower Saxony (DE)

PostPosted: Fri Mar 27, 2026 4:10 pm
Reply with quote

Hello,

I've got a little blockade, maybe my question is pretty easy...

I've got a table with several integer but nullable columns.
Any of them can contain some values with special meaning (i.e. NULL, 0, 99999) or any other ordinary valid integer values.
I'd like to analyse the columns for their content so the expected result is i.e.
Code:

Value-grp   col_1   col_2   col_3
 NULL       1,111   2,222   3,333
    0       4,444   5,555   6,666
99999       7,777   8,888   9,999
valid      11,111  22,222  33,333


Of cause the value group are DECODEd values of the real content of the three columns, in this case of type character(5).

Has anybody of you a quick idea how to solve my problem.

I know about SUM(DECODE(<col>, <value>, 1, 0)) and GROUP BY, but this transponses the result so it is not what I'm looking for.

Thank you all for your support and your hints.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2286
Location: USA

PostPosted: Fri Mar 27, 2026 5:16 pm
Reply with quote

I tried to read the post three times, but yet not able to understand the initial idea: what and how needs to be verified/analyzed?

Please, at least present here:
- the exact sample of you test input data,
- the code sample you have tried,
- the result you expected to receive,
- the result actually produced by your code.

Sorry.
Back to top
View user's profile Send private message
Auryn

New User


Joined: 11 Jan 2006
Posts: 88
Location: Lower Saxony (DE)

PostPosted: Fri Mar 27, 2026 5:27 pm
Reply with quote

One table has among others three cols of type nullable integer so the contained values can be NULL, 0, 99999 or any other "valid"

I'd like to know how often col1 has no value (NULL), the value 0, the value 99999 or any other value.
The same for 2nd and 3rd col.

But think I found a solution. Gimme some minutes...
Back to top
View user's profile Send private message
Auryn

New User


Joined: 11 Jan 2006
Posts: 88
Location: Lower Saxony (DE)

PostPosted: Fri Mar 27, 2026 5:36 pm
Reply with quote

Code:
WITH wertgru (      wert                           )
  AS (      SELECT '1-  NULL' FROM sysibm.sysdummy1
      UNION SELECT '2-     0' FROM sysibm.sysdummy1
      UNION SELECT '3- 99999' FROM sysibm.sysdummy1
      UNION SELECT '4-valid ' FROM sysibm.sysdummy1)
   , werttab (      col_1
                 ,  col_2
                 ,  col_3
  AS (SELECT DECODE(col_1  , NULLIF(0, 0), '1-  NULL'
                           ,           0 , '2-     0'
                           ,       99999 , '3- 99999'
                                         , '4-valid ')
           , DECODE(col_2  , NULLIF(0, 0), '1-  NULL'
                           ,           0 , '2-     0'
                           ,       99999 , '3- 99999'
                                         , '4-valid ')
           , DECODE(col_3  , NULLIF(0, 0), '1-  NULL'
                           ,           0 , '2-     0'
                           ,       99999 , '3- 99999'
                                         , '4-valid ')
        FROM schema.mytable                           )
SELECT    wrt.wert
        , SUM(DECODE(tab.col_1, wrt.wert, 1, 0))
                     anz_col_1
        , SUM(DECODE(tab.col_2, wrt.wert, 1, 0))
                     anz_col_2
        , SUM(DECODE(tab.col_3, wrt.wert, 1, 0))
                     anz_col_3
  FROM    wertgru  wrt
        , werttab  tab
 GROUP BY GROUPING SETS ((wrt.wert), ())
  WITH UR;

wert-suffix #- only for ordering

I know, contains a cartesian product of both cte.

Feel free to provide a more performant or "better" solution.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2286
Location: USA

PostPosted: Fri Mar 27, 2026 7:19 pm
Reply with quote

Auryn wrote:
One table has among others three cols of type nullable integer


First of all: your sample demonstrates the table WITH FOUR COLUMNS, not three...

Please, at least present here:
- the exact sample of you test input data,
(- the code sample you have tried, - done, but the idea is unclear)
- the result you expected to receive,
- the result actually produced by your code.
Back to top
View user's profile Send private message
View previous topic : : View next topic  
Post new topic   Reply to topic All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts DB2 Query to get the count of Unique ... DB2 2
No new posts To join 2 tables and to join 3rd tabl... DB2 8
No new posts Create a specific record/file based o... SYNCSORT 8
No new posts Extract ISPF table column headings CLIST & REXX 2
No new posts how to eliminate null indicator value... DB2 7
Search our Forums:


Back to Top