Subject Help with Stored Procedure
Author Don Gollahon
I'm trying to return a result set that shows the difference of a
quantity between the current record and the previous record. The
following isn't working properly. Any ideas would be appreaciated:

ALTER PROCEDURE TRENDING( PFROM DATE
, PTHRU DATE )
RETURNS ( CARRIER VARCHAR( 6 )
, USAGE_DATE DATE
, NPA CHAR( 3 )
, NXX CHAR( 3 )
, MSG INTEGER
, MSG_T INTEGER
, MINUTES DECIMAL ( 18, 2 )
, MIN_T DECIMAL ( 18, 2 )
, MIN_DIFF DECIMAL ( 18, 2 ) )
AS
DECLARE VARIABLE TMINS DECIMAL(18,2);
BEGIN
FOR SELECT CARRIER
, USAGE_DATE
, NPA
, NXX
, SUM(MSG) AS MSG
, SUM(MSG_T) AS MSG_T
, SUM(MINUTES) AS MINUTES
, SUM(MIN_T) AS MIN_T
FROM MYTABLE
WHERE
(CALL_DATE >= :PFROM) AND (CALL_DATE <= :PTHRU)
GROUP BY
CARRIER
, USAGE_DATE
, NPA
, NXX

INTO
CARRIER
, USAGE_DATE
, NPA
, NXX
, MSG
, MSG_T
, MINUTES
, MIN_T
DO
begin

SELECT MINUTES
FROM MYTABLE T1
WHERE T1.CARRIER = :CARRIER
AND T1.NPA = :NPA
AND T1.NXX = :NXX
AND T1.USAGE_DATE =
(SELECT NEWDATE
FROM INCMONTH(:USAGE_DATE, -1))
INTO
TMINS;

IF (:TMINS IS NULL) THEN
TMINS = :MINUTES;

MIN_DIFF = :MINUTES - :TMINS;

SUSPEND;

end
END

The above compiles and runs but always uses the minutes in the First
record returned to do the subtraction in all other records. Note the
IncMonth is a stored procedure that returns the date so many months from
the given date. I've tested it separately and it works.

Thanks


Don Gollahon
gollahon@...
"The Original GenSoft Prodigal"