DB2 Stored Procedure Calculate Days per month dynamically -



DB2 Stored Procedure Calculate Days per month dynamically -

how write db2 stored procedure calculate dpm logic.

for example, between jan 1, 2014 sept 30, 2014 – 9 records displayed (one record per month). each record, dpm (days per month) needs calculated @ runtime, dynamically , updated in formula :

select (((dpm*24) / 2400) * 123) / dpm xxx-table date between '2014-09-01' , '2014-01-01'

how ensure right dpm gets updated in above mentioned formula

expected output

jan - (((31*24) / 2400) * 123) / 31 feb -(((28*24) / 2400) * 123) / 28 march - (((31*24) / 2400) * 123) / 31 apr - (((30*24) / 2400) * 123) / 30 may - (((31*24) / 2400) * 123) / 31 june - (((30*24) / 2400) * 123) / 30 july - (((31*24) / 2400) * 123) / 31 august - (((31*24) / 2400) * 123) / 31 september - (((30*24) / 2400) * 123) / 30

as @mustaccio said, formula simplifies downwards 1.23, moving first dpm multiplication "out" of first fraction:

(dpm * (24/2400) * 123) / dpm

then, dpm's cancel each other out, leaving

(24/2400) * 123

which constant 1.23

however, if need days per month calculation, utilize recursive query build out list of months, , utilize last_day combined day scalar functions. i'm assuming you're using db2 linux/unix/windows, , think last_day added in 9.7

with dates (dte, lvl) ( select cast(@begindate date), 0 sysibm.sysdummy1 union select dte + 1 months, lvl +1 sysibm.sysdummy1, dates dte + 1 months <= @enddate ) select day(last_day(dte)) dates

stored-procedures db2 days

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -