Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: What went wrong with this Function First?
I really need to output a value based on whether it's a first record by
its by-variable. So that's why I need to do it with a function. Thanks
anyways.
-DP
Charles Hooper wrote:
> dennis.pong_at_gmail.com wrote:
> > Hi,
> >
> > I'm trying to flag the first record within each group of individual_id
> >
> > For example,
> > INDIVIDUAL_ID FIRST_FLAG
> > 1447620 7/30/2005 C 1
> > 1447620 7/21/2005 C 0
> > 1447620 7/15/2005 D 0
> > 1447620 6/30/2005 D 0
> > 1840354 12/1/2006 Z 1
> > 1840354 11/28/2006 D 0
> > 1840354 11/28/2006 D 0
> > 1840354 11/28/2006 D 0
> >
> > HERE IS MY FUNCTION
> >
> > CREATE OR REPLACE Function First
> > ( individual_id IN number )
> > RETURN number
> >
> > IS
> > stored_ind_id number(8);
> > ind_first number(1);
> >
> >
> > cursor c1 is
> > select individual_id, activity_date , lead_grade
> > from IND_ACTS
> > GROUP BY individual_id, activity_date , lead_grade
> > ORDER BY individual_id, activity_date desc, lead_grade;
> >
> >
> > BEGIN
> >
> > stored_ind_id := 0;
> > ind_first := 0;
> >
> >
> > FOR IND_ACTS in c1
> > LOOP
> > If stored_ind_id <> individual_id then
> > stored_ind_id := individual_id;
> > ind_first := 1;
> >
> > END IF;
> >
> >
> > END LOOP;
> >
> > RETURN ind_first;
> >
> > END;
> >
> >
> > Don't know if someone could point out the possible flaws of this
> > function that it doesn't work as expected.
> >
> > Thanks,
>
>> INSERT INTO IND_ACTS VALUES (1840354,'28-NOV-2006','D');
> INSERT INTO IND_ACTS VALUES (1447620,'30-JUL-2005','C');
> INSERT INTO IND_ACTS VALUES (1447620,'21-JUL-2005','C');
> INSERT INTO IND_ACTS VALUES (1447620,'15-JUL-2005','D');
> INSERT INTO IND_ACTS VALUES (1447620,'30-JUN-2005','D');
> INSERT INTO IND_ACTS VALUES (1840354,'01-DEC-2006','Z');
> INSERT INTO IND_ACTS VALUES (1840354,'28-NOV-2006','D');
>
>
>
> INDIVIDUAL_ID ACTIVITY_DATE LEAD_GRADE
> 1447620 30-JUL-2005 00:00:00 C
> 1447620 21-JUL-2005 00:00:00 C
> 1447620 15-JUL-2005 00:00:00 D
> 1447620 30-JUN-2005 00:00:00 D
> 1840354 01-DEC-2006 00:00:00 Z
> 1840354 28-NOV-2006 00:00:00 D
> 1840354 28-NOV-2006 00:00:00 D
> 1840354 28-NOV-2006 00:00:00 D
>
>
> INDIVIDUAL_ID ACTIVITY_DATE LEAD_GRADE ROW_IN_GRP
> 1447620 30-JUN-2005 00:00:00 D 4
> 1447620 15-JUL-2005 00:00:00 D 3
> 1447620 21-JUL-2005 00:00:00 C 2
> 1447620 30-JUL-2005 00:00:00 C 1
> 1840354 28-NOV-2006 00:00:00 D 4
> 1840354 28-NOV-2006 00:00:00 D 2
> 1840354 28-NOV-2006 00:00:00 D 3
> 1840354 01-DEC-2006 00:00:00 Z 1
>
>
> INDIVIDUAL_ID ACTIVITY_DATE LEAD_GRADE ROW_IN_GRP IS_FIRST
> 1447620 30-JUN-2005 00:00:00 D 4
> 0
> 1447620 15-JUL-2005 00:00:00 D 3
> 0
> 1447620 21-JUL-2005 00:00:00 C 2
> 0
> 1447620 30-JUL-2005 00:00:00 C 1
> 1
> 1840354 28-NOV-2006 00:00:00 D 4
> 0
> 1840354 28-NOV-2006 00:00:00 D 2
> 0
> 1840354 28-NOV-2006 00:00:00 D 3
> 0
> 1840354 01-DEC-2006 00:00:00 Z 1
> 1
>
>