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

  • SEARCH
  • Home
  • 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 8961909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:00:06+00:00 2026-06-15T16:00:06+00:00

Assume these Tables: Group : (Id, Title): {1,G1}, {2,G2}, {3,G3}, {4, G4} Category :

  • 0

Assume these Tables:

Group: (Id, Title): {1,G1}, {2,G2}, {3,G3}, {4, G4}

Category: (Id, Title): {1, Cat1}, {2, Cat2}, {3, Cat3}, {4, Cat4}

Product: (Id, GroupId, CategoryId, Name):

{1, 1, 1, G1C1P1},
{2, 1, 2, G1C2P2},
{3, 1, 2, G1C2P3},
{4, 2, 2, G2C2P4},
{5, 2, 2, G2C2P5},
{6, 3, 1, G3C1P6},
{7, 3, 3, G3C3P7}

So What I need is Count of any Category by Group for the above values is:

Group  Category  Count
----------------------
 G1     Cat1      1
 G1     Cat2      2
 G1     Cat3      0
 G1     Cat4      0
 G2     Cat1      0
 G2     Cat2      2
 G2     Cat3      0
 G2     Cat4      0
 G3     Cat1      1
 G3     Cat2      0
 G3     Cat3      1
 G3     Cat4      0
 G4     Cat1      0
 G4     Cat2      0
 G4     Cat3      0
 G4     Cat4      0

I try this:

SELECT 
        [GR].[Title] AS [Group],
        COUNT([PR].[Id]) AS [Count],
        [CA].[Title]

FROM [dbo].[Group] AS [GR]
FULL OUTER JOIN [dbo].[Product] AS [PR] ON [GR].[Id] = [PR].[GroupId]
FULL OUTER JOIN [dbo].[Category] AS [CA] ON [PR].[CategoryId] = [CA].[Id]
GROUP BY [CA].[Title], [GR].[Title];
GO

But it’s not the exact one, So What is your suggestion?

  • 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-06-15T16:00:07+00:00Added an answer on June 15, 2026 at 4:00 pm

    You need to CROSS JOIN the two tables (Group and Category) to create all possible group-category combinations and then LEFT JOIN to the Product table:

    SELECT 
        gr.Title     AS [Group],
        ca.Title,
        COUNT(pr.Id) AS [Count]
    FROM 
            dbo.[Group] AS gr
        CROSS JOIN 
            dbo.Category AS ca 
        LEFT OUTER JOIN 
            dbo.Product AS pr 
                ON  pr.GroupId = gr.Id
                AND pr.CategoryId = ca.Id 
    GROUP BY 
        gr.Title,
        ca.Title 
    ORDER BY
        gr.Title,
        ca.Title ;
    

    or this way (first GROUP BY in the Products table and then join the derived table):

    SELECT 
        gr.Title             AS [Group],
        ca.Title,
        COALESCE(pr.cnt, 0)  AS [Count]
    FROM 
            dbo.[Group] AS gr
        CROSS JOIN 
            dbo.Category AS ca 
        LEFT OUTER JOIN 
            ( SELECT 
                  GroupId,
                  CategoryId,
                  COUNT(*) AS cnt
              FROM
                  dbo.Product 
              GROUP BY 
                  GroupId,
                  CategoryId
            ) AS pr
                ON  pr.GroupId = gr.Id
                AND pr.CategoryId = ca.Id 
    ORDER BY
        gr.Id,
        ca.Id ;
    

    If the Group(Title) and the Category(Title) columns are unique, the queries are equivalent (except for the ordering).

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

Sidebar

Related Questions

Assume I have these tables, from which i need to display search results in
Hello again Stackoverflow people! Assume I have these words: smartphones, smartphone I want to
Consider these 2 pieces of code (you can assume execeptionObj is of type Object
Let's assume I have a database with two tables: categories and articles . Every
I have a query like this: SELECT a.title, a.spec, a.name, l.User AS CreatedBy, DateAdd
I have 2 Tables . CategoryMaster(CATEGORY_ID (primaryKey),Category_Name) and FILE_MASTER (FileId primaryKey,FILE_TITLE,FILE_DESCRIPTION,CATEGORY_ID (refer to Category
Assume that we have a database on MS SQL Server 2008 with 20-30 tables
I'm joining a couple of tables, and aggregating some of the columns via GROUP
assume there is a grid with some points in it just like in the
Assume there are two separate web applications: a sports site with admin backend an

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.