Friday, September 11, 2009

PREVIOUS VALUE RETURN IN QUERY

select dummy_date,nvl(glcode,lag(glcode) over (order by dummy_date)) glcode,nvl(amount,lag(amount) over ( order by dummy_date))amount
from (
select dummy_date,nvl(glcode,lag(glcode) over (order by dummy_date)) glcode,nvl(amount,lag(amount) over ( order by dummy_date))amount
from (
select dummy_date,nvl(glcode,lag(glcode) over (order by dummy_date)) glcode,nvl(amount,lag(amount) over ( order by dummy_date))amount
from (
select dummy_date,nvl(glcode,lag(glcode) over (order by dummy_date)) glcode, nvl(amount,lag(amount) over ( order by dummy_date))amount
from (
SELECT doctdate, SUBSTR (b.acctcode, 1, 5) || ' : ' || b.acctname glcode,
nvl(sum(decode(dbcrcode,'C',jvlcamnt,-jvlcamnt)),0) AMOUNT
FROM sttrndtl a, stchrtac b
WHERE
a.compcode=decode(:p0,'ALL',a.compcode,:p0)
AND doctdate BETWEEN :p11 AND :p12
AND b.acctcode =:p1
AND grupcode = '001'
AND substr(a.acctcode,1,5)= b.acctcode
GROUP BY doctdate, b.acctname,b.acctcode) a,(select trunc(:p11,'MM')-1+rownum dummy_date
from all_objects
where rownum <= (:p12+1)-:p11) b
where a.doctdate(+)=b.dummy_date
ORDER BY dummy_date
)))

---PROCEDURE

CREATE OR REPLACE PROCEDURE dpr_datewise_gl_balance (
p_date OUT DATE,
p_acctcode OUT VARCHAR2,
p_bal OUT NUMBER
) IS
s_datefrom DATE := TO_DATE ('01102008', 'ddmmrrrr');
s_tofrom DATE := TO_DATE ('31102008', 'ddmmrrrr');
s_opnbal NUMBER := 0;
s_curbal NUMBER := 0;
s_incrementbal NUMBER := 0;

CURSOR c_atchrtac IS
SELECT acctcode, acctname
FROM stchrtac
WHERE acctcode IN ('10200')
ORDER BY 1;
BEGIN
FOR i IN c_atchrtac LOOP
SELECT NVL (SUM (DECODE (dbcrcode, 'C', jvlcamnt, -jvlcamnt)), 0)
INTO s_opnbal
FROM sttrndtl
WHERE SUBSTR (acctcode, 1, 5) = i.acctcode AND doctdate < s_datefrom;

WHILE s_datefrom <= s_tofrom LOOP
p_acctcode := i.acctcode;
p_date := s_datefrom;

SELECT (NVL (SUM (DECODE (dbcrcode, 'C', jvlcamnt, -jvlcamnt)), 0))
INTO s_curbal
FROM sttrndtl
WHERE SUBSTR (acctcode, 1, 5) = i.acctcode AND doctdate = p_date;

s_incrementbal := s_incrementbal + s_curbal;
p_bal := s_opnbal + s_incrementbal;
DBMS_OUTPUT.put_line (p_date || ' ' || p_acctcode || ' ' || p_bal);
s_datefrom := s_datefrom + 1;
END LOOP;
END LOOP;
END;

No comments: