views:

52

answers:

2

Hello

im trying to make a Function that retruns a value.

in my function i have this script:

WITH t_new AS
    (
      SELECT PersIDOLD, PersIDNEW, RightsMUT,
      SUM(gap) over(ORDER BY PersIDOLD, PersIDNEW) grp

      FROM
        (
          SELECT h1.*,
            CASE
              WHEN h1.PersIDNEW = lag(h1.PersIDNEW) 
                                    over(ORDER BY h1.PersIDOLD, h1.PersIDNEW)
                OR h1.PersIDOLD = lag(h1.PersIDOLD) 
                                    over(ORDER BY h1.PersIDOLD, h1.PersIDNEW)
              THEN 0
              ELSE 1
            END gap
          FROM HelpTable1 h1

        )
    )
  SELECT PERSIDNEW, RIGHTSMUT
  FROM t_new
  WHERE grp =
    (
      SELECT grp FROM t_new WHERE PersIDNEW = 12 -- going to be a variable
    )

the return is like this:

now i want to return row 3 coze it has a 1 in row RightsMUT

how can i do this? i tried to do it with 2 With but oracle does not support 2 with yet :/

greets Auro

A: 

Let me try to guess - pls check if the following gives you what you need:

WITH t_new AS
(
  SELECT PersIDOLD, PersIDNEW, RightsMUT,
  SUM(gap) over(ORDER BY PersIDOLD, PersIDNEW) grp

  FROM
    (
      SELECT h1.*,
        CASE
          WHEN h1.PersIDNEW = lag(h1.PersIDNEW) 
                                over(ORDER BY h1.PersIDOLD, h1.PersIDNEW)
            OR h1.PersIDOLD = lag(h1.PersIDOLD) 
                                over(ORDER BY h1.PersIDOLD, h1.PersIDNEW)
          THEN 0
          ELSE 1
        END gap
      FROM HelpTable1 h1

    )
) 
SELECT PERSIDNEW, RIGHTSMUT
FROM t_new
WHERE PersIDNEW = 12 AND RightsMUT = 1
Unreason
this does return nothing but i found a way how to do it...and it was so simple :/
Auro
ok, if you solved it either update the question to show the answer (you might get some comments on it) or post it as the answer to your own question.
Unreason
A: 

my Solution:

  WITH t_new AS
    (
      -- Alles durch gehen und
      SELECT PersIDOLD, PersIDNEW, RightsMUT, SUM(gap) over(ORDER BY PersIDOLD, PersIDNEW) grp
      FROM
        (
          SELECT h1.*,
            CASE
              WHEN h1.PersIDNEW = lag(h1.PersIDNEW) over(ORDER BY h1.PersIDOLD, h1.PersIDNEW)
                OR h1.PersIDOLD = lag(h1.PersIDOLD) over(ORDER BY h1.PersIDOLD, h1.PersIDNEW)
              THEN 0
              ELSE 1
            END gap
          FROM LUTAXKT.HILFSTABELLE1UMSETZUNG h1
            --WHERE h1.updateflag = 0
        )
    )
  SELECT PersIDNEW, RightsMUT
  FROM t_new
  WHERE  RightsMUT = 1 and
   grp =
    (
      SELECT grp FROM t_new WHERE PersIDNEW = 12
    )

greets
Auro

Auro