Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:06:19+00:00 2026-05-26T04:06:19+00:00

I have the following tables: Category table which has an ID column, a description

  • 0

I have the following tables:

  1. Category table which has an ID column, a description column, and category parent ID columns as follows:


cat_id | cat_description | cat_pid
——-+—————–+——–
1 | State | 0
2 | Texas | 1
3 | California | 1
4 | Title | 0
5 | Engineer | 4
6 | Lawyer | 4
7 | Farmer | 4
8 | Credit Card | 0
9 | Visa | 8
10 | Master Card | 8
…

2. Customer table which has customer ID and name as follows:


cust_id | cust_name
——–+———–
111111 | John
222222 | David
333333 | Chris
444444 | Mark
…

3. Category_Has_Customer which is many to many relationship between Customer and Category.


chc_cust_id | chc_cat_id
————+———–
111111 | 2
111111 | 5
111111 | 9
222222 | 7
222222 | 3
333333 | 6

The category has only two levels depth.

In my application, a customer could have zero or more categories. I would like to display some or all of the categories, which the customer has. For example if I choose to display all categories I would like to have the following table:


cust_name | State | Title | Credit Card
———-+————+———-+————
John | Texas | Engineer | Visa
David | California | Farmer |
Chris | | Lawyer |
Mark | | |

I should also be able to display certain categories, for example Title and Credit Card only:


cust_name | Title | Credit Card
———-+———-+————
John | Engineer | Visa
David | Farmer |
Chris | Lawyer |
Mark | |

I tried to do it with LEFT JOIN’s, something like:

SELECT c1.cust_id, c1.cust_name, t1.cat_desc as State
FROM Customer c1, Category_has_Customer chc
LEFT JOIN Category t1 ON t1.cat_pid = '1' AND chc.chc_cat_id = t1.cat_id
WHERE c1.cust_id = chc.chc_cust_id

But it didn’t help since I got duplicated rows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:06:19+00:00Added an answer on May 26, 2026 at 4:06 am

    I’ll give this a go, should work on all sql implemtations but I’ve done in in t-sql:

    With this data

    declare @Category table (cat_id int, cat_description varchar(20), cat_pid int)
    declare @Customer table (cust_id int, cust_name varchar(20))
    declare @Customer_Has_Category table (chc_cust_id int, chc_cat_id int)
    
    insert into @Category (cat_id, cat_description, cat_pid) values (1,'State',0)
    insert into @Category (cat_id, cat_description, cat_pid) values (2,'Texas',1)
    insert into @Category (cat_id, cat_description, cat_pid) values (3,'California',1)
    insert into @Category (cat_id, cat_description, cat_pid) values (4,'Title',0)
    insert into @Category (cat_id, cat_description, cat_pid) values (5,'Engineer',4)
    insert into @Category (cat_id, cat_description, cat_pid) values (6,'Lawyer',4)
    insert into @Category (cat_id, cat_description, cat_pid) values (7,'Farmer',4)
    insert into @Category (cat_id, cat_description, cat_pid) values (8,'Credit Card',0)
    insert into @Category (cat_id, cat_description, cat_pid) values (9,'Visa',8)
    insert into @Category (cat_id, cat_description, cat_pid) values (10,'Master Card',8)
    
    insert into @Customer (cust_id, cust_name) values (111111, 'John')
    insert into @Customer (cust_id, cust_name) values (222222, 'David')
    insert into @Customer (cust_id, cust_name) values (333333, 'Chris')
    insert into @Customer (cust_id, cust_name) values (444444, 'Mark')
    
    insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (111111, 2)
    insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (111111, 5)
    insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (111111, 9)
    insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (222222, 7)
    insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (222222, 3)
    insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (333333, 6)
    

    This query

    select cust_name, MAX(State) as State, MAX(Title) as Title, MAX(CreditCard) as CreditCard
    from
    (
    select 
    c.cust_name,
    (case when cat.cat_pid = 1 then cat_description else '' end) as State,
    (case when cat.cat_pid = 4 then cat_description else '' end) as Title,
    (case when cat.cat_pid = 8 then cat_description else '' end) as CreditCard
    from @Customer c 
    left outer join @Customer_Has_Category chc on c.cust_id = chc.chc_cust_id
    left outer join @Category cat on chc.chc_cat_id = cat.cat_id
    ) as subq
    group by cust_name
    

    Gives

    cust_name            State                Title                CreditCard
    -------------------- -------------------- -------------------- --------------------
    Chris                                     Lawyer               
    David                California           Farmer               
    John                 Texas                Engineer             Visa
    Mark   
    

    If you want to take out the State just remove it from the select statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table results, which has he following columns: ID TestCase Platform 1
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
I have the following tables: TABLE: category catid catfatherid catname 1 0 United States
I have the following hierarchical table: Table Category: CategoryId, ParentCategoryId, CategoryName 1, null, SomeRoot
I have 2 tables with the following structure.. category { sub_cat_id - numeric sub_cat_name
I have the following tables in my database that have a many-to-many relationship, which
I am currently developing a tool which has the following tables and the respective
I have a two tables as follows: product category(t1): id name product master(t2): id
In my PHP application, I have a mysql table of articles which has the
I have the following tables: Table categories { -id- -name- 1 Action 2 Comedy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.