Solved

Multi Level Category Stored Procedure Question

Posted on 2004-09-06
7
378 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
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…

756 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