Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parent / Child Query Assistance

0 views
Skip to first unread message

Albert Frausto

unread,
Dec 23, 2009, 10:37:01 PM12/23/09
to
I have one table that contains the parent / child data for vehicle
makes/models. I'm trying to create the following results which will populate
a listbox. Here's the table:

Table: directory_categories

category_id category_name category_id_parent
1 Acura NULL
2 Ford NULL
3 GM NULL
4 Model T 2
5 Model A 2
6 Truck 2

Target results,

catsub_id catsub_name
1 Acura
2 Ford
4 Ford > Model T
5 Ford > Model A
6 Ford > Truck
3 GM

I've tried the following query but it's not quite giving me the correct
results.

SELECT C1.category_id AS catSub_ID,
C1.category_name+Nz('->'+C2.category_name,'') AS CatSub_Name
FROM directory_categories AS C1 LEFT JOIN directory_categories AS C2 ON
C1.category_id_parent = C2.category_id
ORDER BY C1.category_name;

I get the following with the above query:

cat_SubID cat_SubName
1 Acura
2 Ford
3 GM
4 Model T > Ford (This should be Ford -> Model T)
5 Model A > Ford (This should be Ford -> Model A)
6 Truck > Ford (This should be Ford -> Truck)

Thank you to everyone for their help in advance.

Sylvain Lafontaine

unread,
Dec 24, 2009, 12:32:08 AM12/24/09
to
Pretty obvious:

SELECT C1.category_id AS catSub_ID,
Nz(C2.category_name + '->','') + C1.category_name AS CatSub_Name


FROM directory_categories AS C1 LEFT JOIN directory_categories AS C2 ON
C1.category_id_parent = C2.category_id

ORDER BY Nz(C2.category_name + '->','') + C1.category_name


For the ORDER BY, another solution is also possible:

Order By Case When C2.Category_name is Null Then C1.Category_name Else
C2.Category_name End

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
Independent consultant and remote programming for Access and SQL-Server
(French)


"Albert Frausto" <Albert...@discussions.microsoft.com> wrote in message
news:D6437907-77A1-45B3...@microsoft.com...

Albert Frausto

unread,
Dec 24, 2009, 12:24:01 PM12/24/09
to
You're correct it was pretty obvious, I was too close to the trees and
couldn't see the forest. Thanks.

Is there another solution that doesn't use the NZ function in the SELECT
statement?

"Sylvain Lafontaine" wrote:

> .
>

Sylvain Lafontaine

unread,
Dec 24, 2009, 1:47:34 PM12/24/09
to
What's the point of replacing the NZ function?

You could use a Case statement but that would involve testing for Is Null
inside, so it's pretty the same thing and there is also the Coalesce
statement.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
Independent consultant and remote programming for Access and SQL-Server
(French)


"Albert Frausto" <Albert...@discussions.microsoft.com> wrote in message

news:8EF1F271-B93F-41BB...@microsoft.com...

Debra

unread,
Dec 28, 2009, 12:40:22 PM12/28/09
to

"Albert Frausto" <Albert...@discussions.microsoft.com> wrote in message
news:D6437907-77A1-45B3...@microsoft.com...

Debra

unread,
Dec 28, 2009, 1:10:36 PM12/28/09
to

"Albert Frausto" <Albert...@discussions.microsoft.com> wrote in message
news:D6437907-77A1-45B3...@microsoft.com...

Frausto

unread,
Mar 9, 2010, 12:47:01 PM3/9/10
to
The reason for replacing NZ function is because I'm using ADO to display the
results in a drop-down menu list using ASP and the NZ function is not
recognized.

Thanks again for all your help.

"Sylvain Lafontaine" wrote:

> .
>

Sylvain Lafontaine

unread,
Mar 9, 2010, 2:17:06 PM3/9/10
to
> The reason for replacing NZ function is because I'm using ADO to display
> the
> results in a drop-down menu list using ASP and the NZ function is not
> recognized.

Without seeing the exact code that you are using, it's impossible to say
what's happening here but in all case, I'm very surprised to hear that the
execution of the Nz() function on the SQL-Server will be dependant on the
data communication interface used. Whatever you are using ADO or DAO with
either ODBC or OLEDB, the execution of the NZ() function should be
independant of that; so I'm suspecting that you are making some sort of
other error.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
Independent consultant and remote programming for Access and SQL-Server
(French)


"Frausto" <Fra...@discussions.microsoft.com> wrote in message
news:8E0CA802-E0AC-4C90...@microsoft.com...

0 new messages