I need help creating a Perl function that will dynamically create the following SQL statement:
Each one of the mth_XX buckets contains a history quantity value.
Code:
SELECT GREATEST(ROUND((SUM(mth_01 + mth_02 + mth_03
+ mth_04 + mth_05 + mth_06
+ mth_07 + mth_08 + mth_09
+ mth_10 + mth_11 + mth_12) / 365 ) * 120 )
, ROUND (SUM (mth_08 + mth_09 + mth_10 + mth_11))) QTY, b.p_dlrnet, b.p_dlrsplrcode
FROM pac.HISTORYREPORTMONTHLYBYSKU a
, stsc.sku b
WHERE a.loc = 'K531'
AND a.dmdunit = '5-280X'
AND a.loc = b.loc
AND a.dmdunit = b.ITEM
GROUP BY a.loc
, a.dmdunit
, b.p_dlrnet, b.p_dlrsplrcode
The only part of this query that changes dynamically is this part: mth_08 + mth_09 + mth_10 + mth_11
The months to include here are based on the number of days in the query, which in this case is 120. Yes, I am assuming all months have 30 days.
The monthly buckets to use are determined by the current month. If the user enters a # of days = 60 and the current month is 11, the placeholder string should be "mth_10, mth_11". If they enter 90, the placeholder string should be "mth_09, mth_10, mth_11". If they enter 75 , the placeholder string should be "mth_09, mth_10, mth_11/2" (note that I want 1/2 of mth_11 value)
I need some way of taking this number, dividing it by 30 and get the # of monthly buckets to use and then creating this placeholder string.