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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:18+00:00 2026-05-27T05:51:18+00:00

I am attempting to join two tables (purchases and purchase attributes), and then group

  • 0

I am attempting to join two tables (purchases and purchase attributes), and then group based on the join, however NULL values are generated during the join as not every entry in table A has a corresponding ‘attributes’ in table B – this then kills my ‘group by’.

My actual implementation is more complicated than this but here is a trimmed concept of what I am trying to achieve:

I have the following Tables:

purchases:

|purchase_id | products_id |   qty    |
|      1     |   10        |   100    |
|      2     |   11        |   100    |
|      3     |   10        |   300    |
|      4     |   12        |    50    |

purchase_attributes:

|purchase_attributes_id | purchase_id | options_id |
|         1             |      1      |     5      |
|         2             |      3      |     5      |
|         3             |      4      |     5      |

Now, my goal is to calculate the total qty of a particular product, grouping by options_id.
So, I have the following sql:

select products_id, options_id, SUM(qty) 
from purchases
left join purchase_attributes using(purchase_id)
group by products_id, options_id

Which works for all cases where the purchase_id is listed in the purchase_attributes table, however for the case of purchase_id 2, which does not appear in purchase_attributes, it does not display in my result set.

Example result set:

| products_id  | options_id  |  SUM(qty)  |
|     10       |     5       |    400     |
|     12       |     5       |     50     |

However I would like to receive the following result set:

| products_id  | options_id  |  SUM(qty)  |
|     10       |     5       |    400     |
|     11       |     NULL    |    100     |
|     12       |     5       |     50     |

I have tried a few things, including Coalesce inside the group by and Union(ing) two select statements together, however I cannot seem to get the correct results.

Any ideas? thanks in advance.

  • 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-27T05:51:18+00:00Added an answer on May 27, 2026 at 5:51 am

    This does produce exactly your desired results.

    SELECT products_id,options_id,SUM(qty)
    FROM   (SELECT 1 AS purchase_id,10 AS products_id,100 AS qty
            UNION ALL
            SELECT 2,11,100
            UNION ALL
            SELECT 3,10,300
            UNION ALL
            SELECT 4,12,50) purchases
           LEFT JOIN (SELECT 1 AS purchase_attributes_id,1 AS purchase_id,5 AS
                             options_id
                      UNION ALL
                      SELECT 2,3,5
                      UNION ALL
                      SELECT 3,4,5) purchase_attributes USING(purchase_id)
    GROUP  BY products_id,options_id  
    

    Please provide an example that actually demonstrates your issue.

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

Sidebar

Related Questions

I am attempting to join two tables by comparing and matching values in separate
I am attempting to delete all rows in two dependent tables based on a
I'm attempting to perform a full outer join on two tables that are not
I am attempting to perform an inner join on two tables so that I
I am attempting to do an UPDATE with a JOIN. I have two tables:
This is driving me nuts. I have two tables that I am attempting to
I have a two tables similar to the following: I am attempting to create
I am attempting to retrieve data from two tables and echo the results out,
I am attempting to call the Apache Commons StringUtils.join() method from within a Groovy
I'm creating a small forum. Attempting to run SElECT... JOIN... query too pick up

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.