Solved

Multi Level Category Stored Procedure Question

Posted on 2004-09-06
7
369 Views
Last Modified: 2008-03-17
I am having problems with this query....

I want to modify it to do the same thing but for each sublevel add a ':' between the category/subcategory/child cat etc... and have unlimited nesting capabilities

SET CONCAT_NULL_YIELDS_NULL off  
select isnull(subcats.id,categories.id),subcats.id,(case subcats.id when null then categories.catname1 else categories.catname1 + ':' + subcats.catname1 end) from  
categories  
left join categories as subcats on categories.id=subcats.topcatid  
where categories.topcatid=0  
order by categories.catname1,subcats.catname1  

Thanks..
0
Comment
Question by:moruda
7 Comments
 
LVL 32

Expert Comment

by:jadedata
Comment Utility
Greetings moruda!

  what platform or db application are you writing this for?

regards
:)-j-
0
 
LVL 15

Expert Comment

by:jdlambert1
Comment Utility
From looking at your previous questions, I take it you're using SQL Server, but please post the structure of the categories table, some sample data from the categories table, and a sample of what you want the query results to look like.
0
 
LVL 4

Expert Comment

by:complexymetron
Comment Utility
Hi!

I used this recursive approach against a Firebird RDBS. I post it anyway, maybe it give's you a hint.
It was for testing only, I didn't use it in a productional environment.

CREATE PROCEDURE GET_KATTREE_STRING (
  KATID Integer)
 returns (
  WGTEXT VarChar(255))
AS
DECLARE VARIABLE FATHERID INTEGER;
DECLARE VARIABLE WAGRUTEXT VARCHAR(50);
DECLARE VARIABLE FATHERTEXT VARCHAR(255);

BEGIN
    IF (KATID = 0) THEN
        EXIT;

    SELECT
        BEZEICHNUNG, FATHERID
    FROM
        ARTKATTREE
    WHERE
        ID = :KATID
    INTO
        :WAGRUTEXT, :FATHERID;

    IF (FATHERID = 0) THEN
        WGTEXT = WAGRUTEXT;
    ELSE
    BEGIN
        EXECUTE PROCEDURE GET_KATTREE_STRING FATHERID
            RETURNING_VALUES FATHERTEXT;
        WGTEXT = FATHERTEXT || ", " || WAGRUTEXT;
    END
    SUSPEND;
END
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:moruda
Comment Utility
MS SQL 2000
categories table data is like this

0 topcatid means a main category

example data
id   catname         topcatid
1    Televisions          0
2    Phones               0
3    Plasma               1
4    LCD                    1
5    14 inch               3

how i want it to show up  order doesnt matter for now
id   category
3  Televisions:Plasma
5  Televisions:Plasma:14 inch
4  Televisions:LCD
2  Phones
0
 
LVL 32

Expert Comment

by:jadedata
Comment Utility
You need to know the lowest level that will play here.  
Unlike code, you can not ask a query to just drill until it hits paydirt and quit when it runs out.

but if you establish the lowest level possible you can write the sql to that level, and correct for nulls along the way
0
 

Author Comment

by:moruda
Comment Utility
Ok, so lets set it at 3 levels
Main, Sub, child
0
 
LVL 32

Accepted Solution

by:
jadedata earned 500 total points
Comment Utility
SELECT A.ID,
  A.catname  &
    (case B.id when not null then ':' & B.catname else '' end) &
    (case C.id when not null then ':' & C.catname else '' end)
  as Catagory

FROM Table1 A,
  LEFT JOIN Table1 B
    LEFT JOIN Table1 C ON B.ID=C.topcatid
  ON A.id=B.topcatid

I'm not 100% on the syntax of the Catagory Expression

What you need in a three level recursive join of the same table that links the topcatid to it respective parent record.
I hope the example shows that clearly even if the syntax needs addtional work.  I usually work in MSAccess which is a bastardization of real sql
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now