SQL - With statement in WHERE claue
HI,
Does a WITH state can live in a WHERE clause?
for instance:
SELECT tbl1.name , tbl1.ID
FROM DBTABLE0001 AS tbl1
WHERE (
exists(
WITH H (super, ID, dept开发者_StackOverflow中文版h) AS
(
SELECT ROOT.parent, ROOT.ID , 0
FROM DBTABLE0001 ROOT
WHERE ROOT.ID = tbl1.ID
UNION ALL
SELECT PARENT.parent, PARENT.ID , CHILD.depth + 1
FROM H CHILD, DBTABLE0001 PARENT
WHERE PARENT.ID = CHILD.super
)
SELECT ID
FROM H
WEHER H.ID = "abcd"
ORDER BY depth
)
)
Thanks, Mo.
You should place your with statements at the begining of your query. then you can use it every where :
WITH H (super, ID, depth) AS
(SELECT ROOT.parent, ROOT.ID , 0
FROM DBTABLE0001 ROOT
WHERE ROOT.ID = DBTABLE0001.ID
UNION ALL
SELECT PARENT.parent, PARENT.ID , CHILD.depth + 1 FROM H CHILD, DBTABLE0001 PARENT
WHERE PARENT.ID = CHILD.super)
SELECT tbl1.name , tbl1.ID
FROM DBTABLE0001 AS tbl1
WHERE (exists(SELECT ID FROM H WEHER H.ID = "abcd" ORDER BY depth))
精彩评论