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 6131029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:55:36+00:00 2026-05-23T16:55:36+00:00

Possible Duplicate: Getting count from sql tables. I have already asked earlier on same

  • 0

Possible Duplicate:
Getting count from sql tables.

I have already asked earlier on same data and tables but this time requirement is slight different. So please do not vote it to close or downvote.
I have 4 tables, as displayed in image 3 tables and another is tbl_Company.:
enter image description here

Now in the map table, the primary keys of the rest three tables. Also in table sub category we have various childs of category, marked with category id 2, but if you see in map we have only 4 items with categoryid 2 and has two unique companies 7 and 8.

So what I want is to display when a category is chosen, all its subcategories will get listed with the number of companies. Like in map table cat id 2 has 4 rows and has 12 sub cats (actually in table its 44). SO my output will have all 44 subcat with displaying 4 sub cat has companies and rest 0. Something like this

SubCategoryName  TotalCompanies
---------------  --------------
Badges, Emblems  0
Fashion scarves  1
…                …

and so on.

I used this query

SELECT     tbl_SubCategory.Name AS SubCategoryName, TotalCompanies = (Select COUNT(CompanyId) From tbl_Company_Category_Map WHERE CategoryId=2 )
FROM       tbl_Category RIGHT JOIN
                      tbl_SubCategory ON tbl_Category.Id = tbl_SubCategory.CategoryId
                      LEFT JOIN 

                      tbl_Company_Category_Map ON tbl_SubCategory.Id = tbl_Company_Category_Map.SUbCategoryId 
WHERE     (tbl_Category.Id = 2)
Group By tbl_SubCategory.Name
ORDER BY tbl_SubCategory.Name

but it returns me 4 companies for all 44 subcategories where as my actual output should be only 4 rows should have companies and rest 0

  • 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-23T16:55:36+00:00Added an answer on May 23, 2026 at 4:55 pm

    This particular query doesn’t seem to require the tbl_Category table:

    SELECT
      SubCategoryName = tbl_SubCategory.Name,
      TotalCompanies  = COUNT(DISTINCT ccm.CompanyId)
    FROM tbl_SubCategory sc
      LEFT JOIN tbl_Company_Category_Map ccm ON sc.Id = ccm.SubCategoryId
    WHERE sc.CategoryId = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: NoClassDefFoundError - Eclipse and Android I'm seeing this question is getting asked
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: JavaScript data formatting/pretty printer I am getting a bit tired of looking
Possible Duplicate: Getting the class name from a static method in Java When you
Possible Duplicate: getting identity row value using ADO.NET Entity This is my example code:
Possible Duplicate: Getting a value from HttpServletRequest.getRemoteUser() in Tomcat without modifying application Short version:
Possible Duplicate: Getting the Specified content into the buffer in c I have a
Possible Duplicate: pthread Function from a Class I am getting an error (Can not
Possible Duplicate: Getting Filename from file descriptor in C Is there a simple and
Possible Duplicate: convert month from name to number I have a simple (yet interesting)

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.