从With子句返回一行

时间:2010-05-21 10:03:37

标签: sql oracle sql-function inline-view

我正在尝试创建一个重新赋值的函数。

在我的函数中,我有这个脚本:

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
    )

回报是这样的:

http://img59.imageshack.us/img59/6733/tablei.gif

现在我要返回row 3 coze它有一个1行RightsMUT

我怎么能这样做? 我尝试用2 With来做,但oracle现在还不支持2。

2 个答案:

答案 0 :(得分:0)

让我试着猜一下 - 请检查以下内容是否能满足您的需求:

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

答案 1 :(得分:0)

我的解决方案:

  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
    )