Solved

Multi Level Category Stored Procedure Question

Posted on 2004-09-06
7
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Expert Comment

by:jadedata
ID: 11991982
Greetings moruda!

  what platform or db application are you writing this for?

regards
:)-j-
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11992625
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
ID: 11992659
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:moruda
ID: 11993539
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
ID: 11997496
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
ID: 11998437
Ok, so lets set it at 3 levels
Main, Sub, child
0
 
LVL 32

Accepted Solution

by:
jadedata earned 500 total points
ID: 11998977
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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

739 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