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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:19:50+00:00 2026-05-23T09:19:50+00:00

I am joining two tables (shipments and returns) and using group by to view

  • 0

I am joining two tables (shipments and returns) and using group by to view totals for certain criteria. The two tables are related via shipment_id. this column is mostly unique, but contains a few duplicates because each shipment can contain more than one item that is also contained in the table.

I’m trying to count all the distinct shipments grouped by warehouse, seller, and size. count(distinct works great, but does not report correct information when used with group by if the range of items being grouped is significant.

The query below returns 7 shipments (added up) 4 returns (also added). While with the small amount of test data I have the return count is correct, there are in actuality 6 distinct shipments, not 7. With this query i’m basically looking at all shipments and joining return information if an item in the shipment has been returned.

select s.warehouse, s.seller, s.size,
count(distinct s.shipment_id) as total_shipments,
count(distinct r.shipment_id) as total_returns
from shipments s
left join returns r
on s.shipment_id = r.shipment_id
group by s.warehouse, s.seller, s.size

I’m concerned that the report I generate won’t be entirely accurate. Is there a work around for this issue? I’ve seen similar issues, but none that really apply. I am using MYSQL

  • 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-23T09:19:50+00:00Added an answer on May 23, 2026 at 9:19 am

    I see a potential problem. If a shipment has multiple items and may end up in duplicate shipment records, that means that it’s possible that the shipment comes from different warehouses or sellers or that the size is different. By grouping by those fields, you risk ending with with shipment being calculated more then once since the shipment_id is technically distinct for that group.

    You could try grouping by s.shipment_id instead of s.warehouse, s.seller, s.size. The problem here is that if the warehouse, seller or size differs, you’ll end up missing one row (for that warehouse/selling/size) but the totals will add up.

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

Sidebar

Related Questions

I have a view that is joining two tables and ordering by the first
I have a relatively simple query joining two tables. The Where criteria can be
I have two tables that are not related using a foreign key but should
ASP.NET 3.5 C# I am joining two tables using Linq. Table names are MapAssets
im joining and retrieving values from two tables using the below query. It displays
I have two tables, User and Company . I have joining them like this:
I have a query which returns the below from joining two tables: +---------------+-----------------+ |
I am joining two tables 'sales/order_item_collection' and 'sales/orders' by 'order_id', so that afterward i
I have two tables that I am joining with the following query... select *
Ok, I have two tables that I am joining and doing a select on

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.