Home » excel » sql – Add totals from rows that are otherwise identical

sql – Add totals from rows that are otherwise identical

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m working on a query that’ll simplify my invoice history (and me comparing receipts) at my boarding kennel.

Instead of doing it manually, I’m working on scraping the data out of the database that the software makes. I think I’ve got a pretty good start, but I’ve got a bit of an issue.

Using Microsoft Access, I’ve added some relationships (which uses INNER JOINS) between a few tables that has all the information, and I have a SQL statement like this:

SELECT 
  Invoices.InDate, 
  Pets.PtPetName, 
  [IIQuantity]*[IIEach] AS Expr1, 
  Clients.CLLastName, 
  InvoiceItems.IIItemCodeDesc, 
  Invoices.InSeq
FROM 
  (((Clients INNER JOIN Pets ON Clients.CLSeq = Pets.PtOwnerCode) 
INNER JOIN 
  Invoices ON Clients.CLSeq = Invoices.InClientSeq) 
INNER JOIN 
  InvoiceItems ON (Invoices.InSeq = InvoiceItems.IIInvSeq) 
               AND (Pets.PtSeq = InvoiceItems.IIPetSequence)) 
INNER JOIN 
  Inventory ON InvoiceItems.IIItemCode = Inventory.InvSeq
WHERE 
  (((Invoices.InDate)>#12/31/2012#))
ORDER BY 
  Invoices.InSeq;

Which gives me something like:

  DATE   | NAME | TOTAL | LNAME | INVSEQ
----------------------------------------
1/1/2013 | ODIE |   15  | SMITH | 12344
1/1/2013 | ODIE |   60  | SMITH | 12344
1/1/2013 | YODA |   10  | QWERT | 12345
1/1/2013 | YODA |   25  | QWERT | 12345
1/1/2013 | YODA |   80  | QWERT | 12345
1/1/2013 | C3PO |   10  | QWERT | 12345
1/1/2013 | C3PO |   80  | QWERT | 12345

In this case, it’s happening because ODIE got a bath as well boarding, and YODA got a bath, boarding, and something else, where C3PO got just a bath and boarding.

What I’d like is :

  DATE   | NAME | TOTAL | LNAME | INVSEQ
----------------------------------------
1/1/2013 | ODIE |   75  | SMITH | 12344
1/1/2013 | YODA |  115  | QWERT | 12345
1/1/2013 | C3PO |   90  | QWERT | 12345

So basically if the petname and invseq are the same in any/all invseqs, add the total amounts (Expr1) up and make it one row.

I’ve just start messing around with SQL and Access to get something like this worked out, and I’m not really sure where to start. I can give more a bit more info on the various tables (but they’re all pretty messy because the kennel software is made to deal with them, not me).

It’s totally necessary to have the WHERE date > because it’s around 15K rows and my computer wasn’t liking that. That cuts it down to a very usable ~500.

Would it be easier to export to excel and do some excel magic instead? I want to get A GOOD solution, not particularly what I thought would be a good solution.

How to&Answers:

It sure sounds like you just want to use the aggregate function sum() to get the total values for each pet.

SELECT 
  Invoices.InDate, 
  Pets.PtPetName, 
  SUM([IIQuantity]*[IIEach]) AS Total, 
  Clients.CLLastName, 
  Invoices.InSeq
FROM 
  (((Clients INNER JOIN Pets ON Clients.CLSeq = Pets.PtOwnerCode) 
INNER JOIN 
  Invoices ON Clients.CLSeq = Invoices.InClientSeq) 
INNER JOIN 
  InvoiceItems ON (Invoices.InSeq = InvoiceItems.IIInvSeq) 
               AND (Pets.PtSeq = InvoiceItems.IIPetSequence)) 
INNER JOIN 
  Inventory ON InvoiceItems.IIItemCode = Inventory.InvSeq
WHERE (((Invoices.InDate)>#12/31/2012#))
GROUP BY Invoices.InDate, 
  Pets.PtPetName, 
  Clients.CLLastName, 
  Invoices.InSeq
ORDER BY  Invoices.InSeq;

You will notice that once the aggregate function was added a GROUP BY clause was added. I removed the inclusion of the InvoiceItems.IIItemCodeDesc column because it did not look like you had it in the desired result. If this description column includes the boarding, bath, etc and the pet has multiple distinct entries then you will have a separate row for each of these distinct code description.