Author Topic: Helpful DB2 queries & shell commands!  (Read 9845 times)

frasert

  • Guest
Helpful DB2 queries & shell commands!
« on: February 18, 2011, 09:35:33 AM »
This is for AIX and DB2, but probably the same concepts could be applied to other environments.

These are some of the queries & commands I've compiled over the years and that I reference on a regular basis....PLEASE make corrections/suggestions as necessary!

------------------------------------------------------------------

1 - Since CMOD stores its internal dates in Unix epoch format, when you run a database query sometimes it is nice to convert each hit found on the fly.  Just pipe the output to a "while read" loop that does the arsdate conversions:

$ db2 -x "select bankno,rdate from spr1 group by bankno,rdate order by bankno,rdate" | while read ; do printf "$REPLY\t" ; arsdate `echo "$REPLY" | awk '{print $2}'` | awk -F'\t' '{print $3}' ; done
001     11837   05/29/02
001     11842   06/03/02
001     12206   06/02/03
...

2 - This query will return the application group's segment date field.  This is helpful in a shell script when you need to query app groups by their segment date field where they don't all use the same name.  The mask field in the arsagfld table is a bitmask that allows for multiple values to be stored in a single field.  The Filter/Index Type as well as the various check boxes in the Field Information tab are captured in this field.

$ db2 "select name from arsagfld where agid in (select agid from arsag where name='REPORT1') and mod((mask-mod(mask,4))/4,2)=1"

NAME              
------------------
rdate            

...

3 - Find application groups that never have had anything loaded in them. This follows the premise that the first load of a report is what generates the segment table.

db2 "select count(*) from arsag left join arsseg on arsag.agid=arsseg.agid where arsseg.agid is null"

...

4 - If the segment table has already been created, but the application group is EMPTY due to expirations or deletes, this query will find those.

db2 "select arsag.name from arsag left join arsload on arsag.agid=arsload.agid where arsload.agid is null order by arsag.name"

...

5 - Find applications that do not have large object set.

db2 "select comp_obj_size from arsapp where comp_obj_size < 102400"

...

6 - Show all folders pointing to an app group:

db2 "select distinct substr(arsfol.name,1,25) as folname, substr(arsag.name,1,20) as agname from arsfol,arsag,arsag2fol where arsfol.fid=arsag2fol.fid and arsag.agid=arsag2fol.agid and arsag.name in ('APPGROUP1')"

...

7 - Show all app groups contained in a folder:

db2 "select distinct substr(arsfol.name,1,25) as folname, substr(arsag.name,1,20) as agname from arsfol,arsag,arsag2fol where arsfol.fid=arsag2fol.fid and arsag.agid=arsag2fol.agid and arsfol.name in ('FOLDER1')"

...

8 - Get average doc size of an application group.

db2 "select avg(cast(doc_len as bigint)) from bda11"

...

More to come??

dr_te_z

  • Guest
Re: Helpful DB2 queries & shell commands!
« Reply #1 on: April 23, 2012, 11:20:58 PM »
I've created the DB2 version:
Code: [Select]
------------------------------------------
---  ars_db2date
----------------
---  author  dr_te_z
----         Februari 2011
----
---- This UDF makes conversions between the date/time format used by OnDemand internal and the date/time format used native by DB2
----   There are 3 versions of this UDF.
----   The db2-system can distinguish them by the format of the input parameter (overloading)
------------------------------------------
----      1 - ars_db2date (BIGINT)    This should be used to convert an ASR date to a db2 timestamp format
----                                  When you need to convert to a db2 date format instead call it like this: date(ars_db2date(BIGINT(123)))
----      2 - ars_db2date (DATE)      This should be used to convert a db2 date      to the ARS internal onDemand format (bigint)
----      3 - ars_db2date (TIMESTAMP) This should be used to convert a db2 timestamp to the ARS internal onDemand format (bigint)
----
------------------------------------------
---- to implement: "db2 -td~ -vf ars_db2date.ddl"
------------------------------------------
drop   function ars_db2date          (timestamp) ~
drop   function ars_db2date          (date)      ~
drop   function ars_db2date          (bigint)    ~
------------------------------------------
create function ars_db2date (ars_date bigint)
returns timestamp
deterministic language sql
begin atomic
   declare #start_of_ars_times   timestamp                               ;
   set     #start_of_ars_times = timestamp ('1970-01-01-00.00.00.000000');
   return (#start_of_ars_times + ars_date seconds)                       ;
end ~
------------------------------------------
---
create function ars_db2date (db2_ts  date)
returns bigint
deterministic language sql
begin atomic
   declare #start_of_ars_times   bigint                                 ;
   set     #start_of_ars_times = days(date('01-01-1970'))               ;
   return ((bigint(days(db2_ts)) - #start_of_ars_times)) * bigint(86400);
end ~
------------------------------------------
---
create function ars_db2date (db2_ts timestamp)
returns bigint
deterministic language sql
begin atomic

   declare #db2_ts_date            date                         ;
   declare #db2_ts_remain_seconds  bigint                       ;   
   set     #db2_ts_date           =  date      (db2_ts)         ;
   set     #db2_ts_remain_seconds = (   hour   (db2_ts) * 3600)
                                     + (minute (db2_ts) *   60)
                                     + (second (db2_ts)       ) ;                                 
   return (ars_db2date(#db2_ts_date) + #db2_ts_remain_seconds ) ;
end ~
------------------------------------------