MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2004, 12:34 AM
balainfo balainfo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 6 balainfo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Category / subcategory

Dear All,

I am doing a auction project and my requirement is

I need to show the Main category along with total count of products in it and its subcategories...

Electrical & Households (11)
Kitchen(5) Dining(6)



In the above out put the Electrical & Household is the main category and Kitchen(5) Dining(6) are subcategories..

My table is

tbl_cat

cid Numeric Identity

Parent Numeric

Cat_Name Varchar(100)



tbl_Procucts


pid Numeric Identity

Cat_Id Numeric (foreign key of tbl_cat)

Prod_Name Varchar(100)



For clear idea pls visit http://www.erealdeal.com

Pls assist me ...

Thanks

Bala

Reply With Quote
  #2  
Old November 5th, 2004, 03:13 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,967 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 59 m 30 sec
Reputation Power: 1024
if you have more than one level of subcategory within a category, you will need to decide whether you want to continue extending the tree downwards with further left outer joins, or whether to use recursion

here is the solution for one level, i.e. major category and one level of subcategory:
Code:
select sum(levelcount)
  from (
       select count(tbl_Procucts.Cat_Id) as levelcount)
         from tbl_cat
       left outer
         join tbl_Procucts
           on tbl_cat.cid
            = tbl_Procucts.Cat_Id
        where cid = number
       union all
       select count(tbl_Procucts.Cat_Id) 
         from tbl_cat cat
       inner
         join tbl_cat subcat
           on cat.cid
            = subcat.Parent 
       left outer
         join tbl_Procucts
           on subcat.cid
            = tbl_Procucts.Cat_Id
        where cat.cid = number
       ) as foo    
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Category / subcategory


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT