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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:13:54+00:00 2026-05-12T05:13:54+00:00

I have two tables, one with all my Branches, and one with all my

  • 0

I have two tables, one with all my Branches, and one with all my sales. The sales table also contains a Sales Rep ID, a Branch ID, a month and a year.

I need a query that will return the sum of a specific rep’s sales for a year, grouped by branch and month, and the query must return 0 if there has been no sales in a branch for that month. I have the following, which does not return 0 if there are no sales:

SELECT
    s.Month,
    b.BranchName,
    SUM(s.InvoiceAmount) AS 'Sales'
FROM 
    Branch b
INNER JOIN
    Sales s ON s.BranchID = b.BranchID
WHERE
    s.Year = 2008
AND
    s.SalesRepID= 11
GROUP BY 
    s.Month,
    b.BranchName
ORDER BY 
    s.Month,
    b.BranchName
  • 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-12T05:13:54+00:00Added an answer on May 12, 2026 at 5:13 am

    You will need to add the “missing” data to be able to join it.

    SELECT
        b.BranchName,
        SUM(ISNULL(s.InvoiceAmount, 0)) AS 'Sales',
        s.Month
    FROM 
        Branch b
        LEFT OUTER JOIN (
                SELECT
            b.BranchID AS BranchID
            , s.SalesRepID AS SalesRepID
            , Months.Month AS Month
            , Years.Year AS Year
            , 0 AS InvoiceAmount
          FROM 
              Sales s
              CROSS JOIN (
                SELECT 1 AS Month
                UNION ALL SELECT 2
                UNION ALL SELECT 3
                UNION ALL SELECT 4
                UNION ALL SELECT 5
                UNION ALL SELECT 6
                UNION ALL SELECT 7
                UNION ALL SELECT 8
                UNION ALL SELECT 9
                UNION ALL SELECT 10
                UNION ALL SELECT 11
                UNION ALL SELECT 12
                ) Months
              CROSS JOIN (
                SELECT 2007 AS Year
                UNION ALL SELECT 2008
                UNION ALL SELECT 2009
                ) Years
              CROSS JOIN Branch b
          UNION ALL SELECT 
            s.BranchID AS BranchID
            , s.SalesRepID AS SalesRepID
            , s.Month AS Month
            , s.Year AS Year
            , s.InvoiceAmount AS InvoiceAmount
          FROM Sales s      
        )s ON s.BranchID = b.BranchID    
    WHERE
        s.Year = 2008
        AND s.SalesRepID= 11
    GROUP BY 
        s.Month,
        b.BranchName
    ORDER BY 
        b.BranchName,
        s.Month
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables. The first one contains all the item informations and the
I have two data tables. One contains all of users identifiable informaiton (username, id,
I have two tables in the database one contains a list of all possible
I'm new to MySQL and PHP. I have two tables, one to hold all
I have two tables in my MySQL database, one is a library of all
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables : one table it matchs and the other is teams.
I have two tables connected with one to many relationship. Parent Table is a
I have two tables. One user table and one table in which results are
I have two tables: one table (okay, it's a view), vComputer, lists many computers

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.