简体   繁体   English

SELECT DISTINCT WO LUMNS and SUM another COLUMN's VALUES

[英]SELECT DISTINCT TWO COLUMNS and SUM another COLUMN's VALUES

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.我曾尝试使用 DISTINCT 和 SUM 子句,但似乎无法进行良好的查询。 It's been long since I have used SQL so some help would really be appreciated.我已经很久没有使用 SQL 了,所以真的很感激一些帮助。

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

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
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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM