Hi Guys,
I'm fairly new to CTE and I would like to ask a little help.
Given the following tables:
ITEM Table
ITEMID(INT) ITEMREFERENCEID(INT)
--------------------------------------
1 48
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
10 58
11 59
12 60
13 61
14 62
15 63
--------------------------------------
ITEM_HIERARCHY table (PARENTNODEID is self reference to NODEID)
NODEID(INT) ITEMID(INT) PARENTNODEID(INT) VISIBLE(INT)
--------------------------------------------------------------------
1 1 NULL 1
2 2 1 1
3 3 1 1
4 4 2 NULL
5 5 2 1
6 6 2 1
7 7 4 1
8 8 6 1
9 9 3 1
10 10 9 1
11 11 9 1
12 12 3 1
13 13 NULL 1
14 14 13 1
15 15 13 2
--------------------------------------------------------------------
If ITEMREFERENCEID = 48 (the anchor), I wanted to return all ITEMS (rows) belonging to the 48 hierarchy (on the ITEM_HIERARCHY table, it should be NODEID 1 to 12)
Can anybody please suggest or show me a query how can this be done using CTE (common table expressions)?
below is the query im trying to work on, it's not getting what I want but it might help any of you guys one way or the other.
Select itemreferenceid item
where itemid in (Select itemid from item_hierarchy
where (Visible IS NULL OR Visible = 1)
and parentnodeid = ( Select nodeid
from item_hierarchy
where itemid = ( Select Itemid
from item
where itemreferenceid=48)))
Thanks in advance,
Yorge
I'm fairly new to CTE and I would like to ask a little help.
Given the following tables:
ITEM Table
ITEMID(INT) ITEMREFERENCEID(INT)
--------------------------------------
1 48
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
10 58
11 59
12 60
13 61
14 62
15 63
--------------------------------------
ITEM_HIERARCHY table (PARENTNODEID is self reference to NODEID)
NODEID(INT) ITEMID(INT) PARENTNODEID(INT) VISIBLE(INT)
--------------------------------------------------------------------
1 1 NULL 1
2 2 1 1
3 3 1 1
4 4 2 NULL
5 5 2 1
6 6 2 1
7 7 4 1
8 8 6 1
9 9 3 1
10 10 9 1
11 11 9 1
12 12 3 1
13 13 NULL 1
14 14 13 1
15 15 13 2
--------------------------------------------------------------------
If ITEMREFERENCEID = 48 (the anchor), I wanted to return all ITEMS (rows) belonging to the 48 hierarchy (on the ITEM_HIERARCHY table, it should be NODEID 1 to 12)
Can anybody please suggest or show me a query how can this be done using CTE (common table expressions)?
below is the query im trying to work on, it's not getting what I want but it might help any of you guys one way or the other.
Select itemreferenceid item
where itemid in (Select itemid from item_hierarchy
where (Visible IS NULL OR Visible = 1)
and parentnodeid = ( Select nodeid
from item_hierarchy
where itemid = ( Select Itemid
from item
where itemreferenceid=48)))
Thanks in advance,
Yorge