An example of my Database: (SQLFiddle link at bottom)
I have several Tables:
Table Login
Table Business
ID_Login_ID_Business (int)
NameOfCompany (varchar)
NameContact(varchhar)
ID_Location_FK (int)
ID_BalancesheetInput_FK (int)
ID_Balancesheetoutput(int)
Table ID_BalancesheetInput
ID
FK_Business_ID (int)
RessourceName (varchar50)
Amount (decimal)
Unit_Fk (int)
TypeOfRessource_FK(int)
Table Units
Unit_id
Unit
Table TypeOfRessource
ID_Type
Type
Sql should check all entries from Buisness where ID_Login_ID_Business(fk) equals the FK_Business_ID in BalancesheetInput, sum all amounts with the same RessourceNam,e and should simply add the typeofRessource checking the right Unit.
Example:
Table Business
ID, NameOfCompany, ID_BalancesheetInput_FK
Table BalancesheetInput
ID, FK_Business_ID, RessourceName, Amount, Unit_FK, TypeOFRessource_FK
Table Units
Unit ID, Unit
Table TypeOFRessource
ID_Type, Type
The following statment tries to catch the Ressources of the selected Business and calculate entries with the same name, such as electricity. They belong to one Company, but something is going wrong.
Everytime I use my sql Statment:
"SELECT DISTINCT "
. "BalancesheetInput.RessourceName AS Rn, "
. "Units.Unit AS En, "
. "SUM(BalancesheetInput.Amount) AS TotalAmount "
. "FROM Business"
. "INNER JOIN BalancesheetInput ON FK_Business_ID = 1 "
. "INNER JOIN Units ON Unit_FK = Unit_ID "
. "INNER JOIN TypeOfRessource ON TypeOf Ressources = 2 "
. "GROUP BY BalancesheetInput.RessourceName ");
Result :
light fuel oil 60 kWh Energie - electricity 600 kWh Energie - thermal energy 2100 kWh
Looks like he added 6 times the amount of each row.
What I want as a result:
light fuel oil 10 kWh Energie - electricity 100 kWh Energie - thermal energy 350 kWh
Here is an SQLFiddle with a working version of all this.
If you need further information, just say what you need to help me solve the problem.
Luk Storms and I came to the same answer; here's the SQLFiddle I made .
Tips: If you SELECT * on your JOIN, you'll see that it's creating a table with six rows for each BalancesheetInput; one for each combination of Name of Company and Energy Type. We used WHERE to narrow down the duplicate rows. This is usually better than SELECT DISTINCT.
Also, "Screws" is miscategorized as "Energie" instead of "Materiel".
It helps to use aliases for the tables. Also to create proper joins on the fields, instead of joining on values that should be in the where clause.
And it's best to make sure that the GROUP BY has all the fields you use in the select (ignoring those used in the functions).
Especially when using MySql.
SELECT
bs.RessourceName AS Rn,
u.Unit AS En,
SUM(bs.Amount) AS TotalAmount
FROM Business b
INNER JOIN BalancesheetInput bs ON bs.FK_Business_ID = b.ID
INNER JOIN Units u ON u.Unit_ID = bs.Unit_FK
INNER JOIN TypeOfRessource t ON t.ID_Type = bs.TypeOFRessource_FK
WHERE b.NameOfCompany = 'Apples'
AND bs.RessourceName = 'electricity'
GROUP BY bs.RessourceName, u.Unit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.