sasrs99 has asked for the wisdom of the Perl Monks concerning the following question:
sub get_BCE_qty { my $num_days_of_supp = shift; my $loc = shift; my $dmdunit = shift; LogMsg("Days of Supply = $num_days_of_supp"); my $qry; my @new_array; my @months_to_sum = ( 12 - ($num_days_of_supp/30) .. 11 ); if ( $num_days_of_supp % 30 == 0 ) { $qry = '"SELECT GREATEST(ROUND((SUM(' . join( ' + ', map { sprintf "mth_%02i", $_ } ( 1 .. 12 ) ) . ") / 365 ) * $num_days_of_supp ), ROUND (SUM (" . join(' + ', map { sprintf "mth_%02i", $_ } @months_to_sum) . '))) QTY, b. p_dlrnet, b.p_dlrsplrcode ' . <<'EOF'; FROM pac.historyreportmonthlybysku a , stsc.sku b WHERE a.loc = ? AND a.dmdunit = ? AND a.loc = b.loc AND a.dmdunit = b.ITEM GROUP BY a.loc , a.dmdunit , b.p_dlrnet , b.p_dlrsplrcode" EOF } else { @new_array = @months_to_sum; shift @new_array; # remove the 1st element since this is the one + we divide by 2 $qry = '"SELECT GREATEST(ROUND((SUM(' . join( ' + ', map { sprintf "mth_%02i", $_ } ( 1 .. 12 ) ) . ") / 365 ) * $num_days_of_supp ), ROUND (SUM (" . sprintf ("mth_%02i", $months_to_sum[0]) . " / 2 + " . join(' + ', map { sprintf "mth_%02i", $_ } @new_array) . '))) QTY, b. p_dlrnet, b.p_dlrsplrcode ' . <<'EOF'; FROM pac.historyreportmonthlybysku a , stsc.sku b WHERE a.loc = ? AND a.dmdunit = ? AND a.loc = b.loc AND a.dmdunit = b.ITEM GROUP BY a.loc , a.dmdunit , b.p_dlrnet , b.p_dlrsplrcode" EOF }; LogMsg($qry); # prepare and execute the query my $sth_C = $dbh->prepare( $qry ) or die $dbh->errstr; + $sth_C->execute( $loc, $dmdunit ); my ($col1, $col2, $col3); $sth_C->bind_col(1, \$col1); $sth_C->bind_col(2, \$col2); $sth_C->bind_col(3, \$col3); $sth_C->fetch(); my $BCE_qty = $col1; my $dlrnet = $col2; my $dlrsupcode = $col3; return $BCE_qty, $dlrnet, $dlrsupcode; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with Dynamic SQL subroutine
by SheridanCat (Pilgrim) on Nov 22, 2006 at 03:08 UTC | |
by sasrs99 (Acolyte) on Nov 22, 2006 at 21:14 UTC | |
by runrig (Abbot) on Nov 27, 2006 at 20:54 UTC | |
by Jenda (Abbot) on Nov 27, 2006 at 23:41 UTC | |
by sasrs99 (Acolyte) on Nov 27, 2006 at 19:33 UTC | |
|
Re: Help with Dynamic SQL subroutine
by jesuashok (Curate) on Nov 22, 2006 at 01:53 UTC |