1

Given two Tables that are Linked together

tbl_Gasoline 
--------------
ID | Type
-------------
1  | Diesel
2  | Kerosene

and

tbl_Expense
-----------------------------
ID | Price  | GasolineType (tbl_Gasoline foreign key)
-----------------------------
1  | 5000   | 1
2  | 4000   | 2
3  | 3000   | 1

I want to have an Output like this

tbl_GasolineExpense
----------------------------
ID |  Price  | Type
----------------------------
1  |  8000   | Diesel
2  |  4000   | Kerosene

I have tried to use a DISTINCT and SUM clauses but I can't seem to make a good query. It's been long since I have used SQL so some help would really be appreciated.

4 Answers 4

1

Try this one

SELECT e.id, SUM(price) AS 'price', g.NAME 
FROM tbl_expense e
INNER JOIN tbl_gasoline g ON e.GasolineType = g.id 
GROUP BY e.id, g.NAME
Sign up to request clarification or add additional context in comments.

Comments

0

Please Try this.

SELECT
e.id,
SUM(price) AS 'price',
g. NAME
FROM
    tbl_expense e
INNER JOIN tbl_gasoline g ON e.GasolineType = g.id
GROUP BY
    g.id,g. NAME

1 Comment

Thanks @S. M Mohiuddin, but I have encountered some error when using the code saying that e.id is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
0
SELECT tbl_Gasoline.ID,SUM(tbl_Expense.Price),tbl_Gasoline.Type 
from tbl_Expense inner join tbl_Gasoline on tbl_Expense.GasolineType = tbl_Gasoline.ID 
group by tbl_Gasoline.ID

Comments

0
select 
  g.ID,
  E.SUM(PRICE) as price,
  e.GasolineType 
from 
  tbl_Gasoline  g,tbl_Expense e 
where 
  g.ID=e.GasolineType group by e.GasolineType

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.