Calculating Summary

Creating tables and queries
Post Reply
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Calculating Summary

Post by allanfermin »

Hi,

I need help to my Office Supplies inventory

Here are the details:

table name: purchases:

date item qty
4/10/2012 notebook 10
4/11/2012 notebook 10
4/11/2012 bond paper 10
4/12/2012 notebook 10


table name: request
date item qty
4/13/2012 notebook 5
4/14/2012 ballpen 5
4/15/2012 bond paper 5
4/16/2001 notebook 5


My only problem is how can get the total stock
like these:

item qty
notebook 5
ballpen 5
bond paper 5
notebook 10

Thank you in advance!
Last edited by allanfermin on Wed Apr 25, 2012 3:41 pm, edited 1 time in total.
OpenOffice3.3
Windows XP
eremmel
Posts: 1080
Joined: Tue Dec 30, 2008 1:15 am

Re: Calculating Summary

Post by eremmel »

Yes I see that you need a computer to do the calculations....

When you have this kind of issues, you need to break it down in sub problems and then build those solutions into the final solution.
So the first is to calculate the totals of purchases and request (this is just typed in and not validated, but hope you have enough understanding of SQL to fix a possible minor typo):

Code: Select all

 SELECT item, sum(qty) AS "PurTotal" FROM purchases GROUP BY item
The totals on request are analogous with total called "ReqTotal".
I assume that you do not deliver requests on items that you never have purchased (that will make things more complicated).
Now we need to combine those two sub-results. There are two possibilities to solve. It think most easy is to save both queries with a logical name like qPurchasesItemTotals and qRequestItemTotals (the 'q' means that it is a named query and not a 't' table or a 'v' view; this helps in understanding the code: queries are part of OOo base document and not part of database).
Now we build a query between those two tables based on the common relation item and add (subtract the totals):

Code: Select all

SELECT p.item AS "Item", (p.PurTotal - r.ReqTotal) AS "CurrentStock" 
FROM qPurchasesItemTotals p LEFT JOIN qRequestItemTotals r ON p.item = r.item 
ORDER BY 1
Note that when you make a typo in a item-name you are lost in this calculation. A slightly improvement is to introduce another table with the item names and give each item an 'id' (unique number) and use that number in both purchases and request tables with mandatory relations, but when this seems to complicated, just forget it ;)

Success,

Erik
It's Microsoft marketing that tells you computers are qualified for non-technicians
W11 22H2 (build 22621), LO 7.4.2.3(x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calculating Summary

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

This is working

I already do this:
SELECT "item", SUM( "quantity" ) AS "PurTotal" FROM "purchases" GROUP BY "item" ORDER BY "item" ASC


but I can't do this one.



SELECT p.item AS "Item", (p.PurTotal - r.ReqTotal) AS "CurrentStock"
FROM qPurchasesItemTotals p LEFT JOIN qRequestItemTotals r ON p.item = r.item
ORDER BY 1
OpenOffice3.3
Windows XP
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

help...
OpenOffice3.3
Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calculating Summary

Post by Villeroy »

allanfermin wrote:help...

Code: Select all

SELECT "Articles"."Name",
  SUM( "Input"."Quantity" ) AS "In",
  SUM( "Output"."Quantity" ) AS "Out", 
  SUM( "Input"."Quantity" - "Output"."Quantity" ) AS "Inventory" 
FROM "Input", "Articles", "Output" 
WHERE "Input"."Article_ID" = "Articles"."ID"
  AND "Output"."Article_ID" = "Articles"."ID" 
GROUP BY "Articles"."Name" 
ORDER BY "Articles"."Name" ASC
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

Villeroy,

I'm sorry it's hard to urdertand the code.
OpenOffice3.3
Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calculating Summary

Post by Villeroy »

allanfermin wrote:Villeroy,

I'm sorry it's hard to urdertand the code.
So you can not do it.
Bye
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

I'm trying to do it but I really I can't understand the code.
OpenOffice3.3
Windows XP
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

Hi Villeroy,

May I ask some question?

SELECT "Articles"."Name", ====>>> what is the articles?
SUM( "Input"."Quantity" ) AS "In", input is purchases?
SUM( "Output"."Quantity" ) AS "Out", output is request?
SUM( "Input"."Quantity" - "Output"."Quantity" ) AS "Inventory"
FROM "Input", "Articles", "Output"
WHERE "Input"."Article_ID" = "Articles"."ID"
AND "Output"."Article_ID" = "Articles"."ID"
GROUP BY "Articles"."Name"
ORDER BY "Articles"."Name" ASC
OpenOffice3.3
Windows XP
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Calculating Summary

Post by RoryOF »

You need to refer to the sample database linked in Villeroy's reply of 10:34 (UTC+1) earlier today; his example will be based on the fields in that database.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

Ok thanks I will try.
OpenOffice3.3
Windows XP
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

any help?
OpenOffice3.3
Windows XP
thomasjk
Volunteer
Posts: 4451
Joined: Tue Dec 25, 2007 4:52 pm
Location: North Carolina

Re: Calculating Summary

Post by thomasjk »

allanfermin wrote:any help?
Please don't keep repeating the same request. Tell us if you have tried what was suggested by Villeroy. If you haven't then we can't help you.
Tom K.
Windows 10 Home version 1803 17134.165
LibreOffice 5.4.7.2
allanfermin
Posts: 9
Joined: Fri Apr 20, 2012 7:09 am

Re: Calculating Summary

Post by allanfermin »

I already try but not success. any simple way?

thanks in advanced...
OpenOffice3.3
Windows XP
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Calculating Summary

Post by RoryOF »

No simple way. You have to read up on it and study. We can no more teach you how to program than we can teach you French in two easy lessons.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calculating Summary

Post by Villeroy »

Thanks to this forum's most valued user "eremel" I learned that my above SELECT query is plain wrong. It sums up the wrong row sets. Topic http://user.services.openoffice.org/en/ ... 011#p72011 has a new version of my database attached.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply