Solved

Multi Level Category Stored Procedure Question

Posted on 2004-09-06
7
386 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

627 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