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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:33:37+00:00 2026-06-10T09:33:37+00:00

I have two tables. The ‘Store’ table is a demographics table, with fields like

  • 0

I have two tables.

The ‘Store’ table is a demographics table, with fields like these:

ID, ParentID, ConsolidationType

ID is the unique identifier for a given store.

ParentID may be either NULL or contain the ID of its parent store if it has a ConsolidationType of ‘Secondary’.

ConsolidationType is either NULL, ‘Primary’, or ‘Secondary’.

The ‘Sales’ table has fields like these:

StoreID, SalesDate, SalesAmount

StoreID refers to an ID in the Store table.


I’m trying to get the total consolidated and individual sales for a given store in a single row of a single query. I’ve written a SQL query like the following:

SELECT 
Store.ID AS 'Store ID',
SUM(IF(YEAR(main.SalesDate) = 2012 AND QUARTER(main.SalesDate) = 1,main.SalesAmount,0)) AS 'Individual Sales',
SUM(IF(YEAR(main.SalesDate) = 2012 AND QUARTER(main.SalesDate) = 1 AND YEAR(secondaries.SalesDate) = 2012 AND QUARTER(secondaries.SalesDate) = 1,main.SalesAmount + secondaries.SalesAmount,0) AS 'Consolidated Sales'
FROM Store 
LEFT JOIN Sales AS 'main' ON Store.ID = main.StoreID 
LEFT JOIN Sales AS 'secondaries' ON Store.ParentID = secondaries.StoreID 
GROUP BY Store.ID

I can’t figure out why this doesn’t work as intended. What am I missing? What’s wrong with my logic?

  • 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-10T09:33:39+00:00Added an answer on June 10, 2026 at 9:33 am

    The problem is that your query is generating a Cartesian-product (-like) result set.

    Basically, a row from ‘main’ is getting repeated multiple times, for each matching row from ‘secondaries’.

    To get the result you want, join to the sales table just one time, and match on both the StoreID and the ParentID.

    To get Individual Sales, only include in the SUM rows where the StoreID matches, something like this:

    SELECT Store.ID AS `Store ID`
         , SUM(IF(main.StoreID = Store.ID,
           IF(YEAR(main.SalesDate) = 2012 AND QUARTER(main.SalesDate) = 1,main.SalesAmount,0)
           ,0)) AS `Individual Sales`
         , SUM(
           IF(YEAR(main.SalesDate) = 2012 AND QUARTER(main.SalesDate) = 1,main.SalesAmount,0)
           ) AS `Consolidated Sales`
      FROM Store 
      LEFT JOIN Sales AS `main` ON main.StoreID = Store.ID OR main.StoreID = Store.ParentID
     GROUP BY Store.ID
    

    UPDATE

    My bad. (DOH!) ParentID is on the Store table, not the Sales table.

    The query above does not return the specified result. (Working on it.)

    I think you had the solution already…

    Use the ParentID column in place of the ID column from the Store table.
    Where the ParentID column is NULL, we use the value from the ID column.

    SELECT IFNULL(Store.ParentID,Store.ID) AS `Store ID`
         , SUM(IF(main.StoreID = Store.ID,
           IF(YEAR(main.SalesDate) = 2012 AND QUARTER(main.SalesDate) = 1,main.SalesAmount,0)
           ,0)) AS `Individual Sales`
         , SUM(
           IF(YEAR(main.SalesDate) = 2012 AND QUARTER(main.SalesDate) = 1,main.SalesAmount,0)
           ) AS `Consolidated Sales`
      FROM Store s
      LEFT JOIN Sales AS `main` ON main.StoreID = Store.ID OR main.StoreID = Store.ParentID
     GROUP BY IFNULL(Store.ParentID,Store.ID)
    


    This query returns the specified result set:

    SELECT t.StoreID AS `Store ID`
         , SUM(IF(t.source='p',
           IF(YEAR(t.SalesDate) = 2012 AND QUARTER(t.SalesDate) = 1,t.SalesAmount,0)
           ,0)) AS `Individual Sales`
         , SUM(
           IF(YEAR(t.SalesDate) = 2012 AND QUARTER(t.SalesDate) = 1,t.SalesAmount,0)
           ) AS `Consolodiated Sales`
      FROM (    
             SELECT 'p' AS source
                  , a.StoreID
                  , a.SalesDate
                  , a.SalesAmount
               FROM Sales a
              UNION ALL
             SELECT 's' AS source
                  , s.ParentID
                  , b.SalesDate
                  , b.SalesAmount
               FROM Sales b
               JOIN Store s ON s.ID = b.StoreID
              WHERE s.ParentID IS NOT NULL
           ) t
     GROUP BY t.StoreID
     ORDER BY t.StoreID
    

    This is not the most efficient, the inline view (or “derived table”) is on the order of the size of the Sales table. It would be more efficient to push the predicates on the date down into the inline view, or to summarize by month, or quarter, in the derived table.

    I would more likely return the “quarter” as part of the result set, to allow me to pull more than one quarter.

    SELECT t.StoreID AS `Store ID`
         , t.SalesQuarter
         , SUM(IF(t.source='p',t.SalesAmount,0)) AS `Individual Sales`
         , SUM(t.SalesAmount) AS `Consolodiated Sales`
      FROM (    
             SELECT 'p' AS source
                  , a.StoreID
                  , ADDDATE(MAKEDATE(YEAR(a.SalesDate),1), INTERVAL FLOOR(MONTH(a.SalesDate)/4) QUARTER) AS SalesQuarter
                  , SUM(a.SalesAmount) AS SalesAmount
               FROM Sales a
          --    WHERE a.SalesDate >= '2012-01-01'
          --      AND a.SalesDate < '2012-04-01'
              GROUP BY a.StoreID, SalesQuarter
              UNION ALL
             SELECT 's' AS source
                  , s.ParentID
                  , ADDDATE(MAKEDATE(YEAR(b.SalesDate),1), INTERVAL FLOOR(MONTH(b.SalesDate)/4) QUARTER) AS SalesQuarter
                  , SUM(b.SalesAmount) AS SalesAmount
               FROM Sales b
               JOIN Store s ON s.ID = b.StoreID
              WHERE s.ParentID IS NOT NULL
          --      AND a.SalesDate >= '2012-01-01'
          --      AND a.SalesDate < '2012-04-01'
              GROUP BY s.ParentID, SalesQuarter
           ) t
     GROUP BY t.StoreID, t.SalesQuarter
     ORDER BY t.StoreID, t.SalesQuarter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables like Parent-Child. In Parent table, there are 7211 records. In
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables connected with one to many relationship. Parent Table is a
I have two tables inside a database. One stores unique userNames and a unique
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay

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.