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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:41:08+00:00 2026-06-04T22:41:08+00:00

I have two tables packages table: packageid, quantity transactions table: transactionid, packageid, quantity Whenever

  • 0

I have two tables

  • packages table: packageid, quantity
  • transactions table: transactionid, packageid, quantity

Whenever a ‘package‘ is obtained, an entry is made in the ‘transactions‘ table, together with the quantity obtained. multiple ‘transactions‘ can be performed and when the package is fully obtained, the sum of all transactions for that package should be equal to the quantity for that package in the packages table.

I want to write a SQL statement to check if a particular packageid has NOT been fully obtained, ie:

  1. no transactions were performed
  2. transactions were performed but the sum of the quantities of all
    transactions is less than the quantity in the package table

how can I achieve this? every join i try to use is only catering for number 2 but does not factor in packages that have had no transactions

i tried:

select package.packageid, package.quantity, sum(transactions.quantity) from package join transactions on package.packageid = transactions.packageid where package.quantity > sum(transactions.quantity)

also tried left join but it does not work. ideally i want all packages listed from the left table in the result with null values on the right where necessary

  • 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-04T22:41:11+00:00Added an answer on June 4, 2026 at 10:41 pm

    If you want to include the packages which have no transactions, then you need to left outer join. This will keep all of the packages in the results and match transactions where transactions exists.

    Then you need to group by package.packageid so that your sum operation only works on transactions records that belong to a certain packageid.

    Then you use a having clause, which is similar to a where clause, only it works with aggregate functions like sum.

    select
      p.packageid,
      p.quantity,
      sum(t.quantity) as transaction_sum
    from
      packages p
      left outer join transactions t on (t.packageid = p.packageid)
    group by
      p.packageid,
      p.quantity
    having
      coalesce(sum(t.quantity), 0) != p.quantity
    ;
    

    The coalesce just says, “take the first non-null value.” So if sum(t.quantity) returns null then it will use 0 instead. This will make sure that packages with a quantity, but no transactions are included in the result set.

    I would advise that you read up on group by and having. They are indispensable when working with sql. In essence, group by splits your data into groups based on criteria they have in common. Then having filters out certain results based on qualities of the group.

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

Sidebar

Related Questions

I have the following two tables: Package id ClientPackage id clientNumber packageId The thing
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables: a schedule table that contains information about how an employee
I have two tables in a database, one lists package and one lists dependencies:
I have two tables. table a references table b I believe. When I try
I have two tables: OutputPackages (master) |PackageID| OutputItems (detail) |ItemID|PackageID| OutputItems has an index
I have two tables namely States, Package_Details. Below are the details of the table.
I have two tables in a Mysql database. PACKAGES: ID (auto_increment) CATEGORY (INT) NAME
I have two MySQL tables, subjects and books, and a third table to define
i have two tables products and reviews each product has several reviews linked by

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.