什么是最好的SQL查询

时间:2017-01-21 23:26:22

标签: java android sql

我有一张名为费用的表格 它有名为id,item,amount,date和category的列

所以我按照我的要求在这些字段中插入值 类别和金额列的实际值为:

Category      Amount

 bill          10
 bill          30
 entertainment 50
 health        5
 bill          60

现在我想要获取类别和金额 所以对于我使用的类别

SELECT DISTINCT category FROM expense WHERE amount>0 ORDER BY category

结果我得到了

  • 纸币

  • 娱乐

  • 健康

对于我正在使用的金额

SELECT * FROM expense WHERE amount>0 ORDER BY category

输出:

  • 10

  • 30

  • 60

  • 50

  • 5

但我希望金额的结果为:

  • 100 *因为账单总额为(10 + 30 + 60)

  • 50

  • 5

1 个答案:

答案 0 :(得分:3)

您想要总结每个类别组的金额值,对吗?

尝试:

select category, sum(amount) 
from expense
where amount > 0
group by category

如果金额不低于0,您可以省略where amount > 0 - 部分。