Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Multi Level Category Stored Procedure Question

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
moruda
Asked:
moruda
1 Solution
 
jadedataMS Access Systems CreatorCommented:
Greetings moruda!

  what platform or db application are you writing this for?

regards
:)-j-
0
 
jdlambert1Commented:
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
 
complexymetronCommented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
morudaAuthor Commented:
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
 
jadedataMS Access Systems CreatorCommented:
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
 
morudaAuthor Commented:
Ok, so lets set it at 3 levels
Main, Sub, child
0
 
jadedataMS Access Systems CreatorCommented:
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now