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:
- no transactions were performed
- 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
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.packageidso that yoursumoperation only works ontransactionsrecords that belong to a certainpackageid.Then you use a having clause, which is similar to a where clause, only it works with aggregate functions like
sum.The coalesce just says, “take the first non-null value.” So if
sum(t.quantity)returnsnullthen it will use0instead. 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.